From 9215f655788d12aff41d7d0f63ca5e8c1687ffcb Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 02:07:15 +0200 Subject: [PATCH 01/29] Update Contents.swift --- .../Contents.swift | 30 +++++++------------ 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/Student Resources/1 - Getting Started/1 - Introduction to Swift and Playgrounds/lab/Lab - Introduction.playground/Pages/1. Exercise - Use Playgrounds.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/1 - Introduction to Swift and Playgrounds/lab/Lab - Introduction.playground/Pages/1. Exercise - Use Playgrounds.xcplaygroundpage/Contents.swift index cd86d9d..c152620 100644 --- a/Student Resources/1 - Getting Started/1 - Introduction to Swift and Playgrounds/lab/Lab - Introduction.playground/Pages/1. Exercise - Use Playgrounds.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/1 - Introduction to Swift and Playgrounds/lab/Lab - Introduction.playground/Pages/1. Exercise - Use Playgrounds.xcplaygroundpage/Contents.swift @@ -1,30 +1,20 @@ /*: - ## Exercise - Use Playgrounds - - The code below prints a few short statements about what you have learned in this lesson. Open the console area and view the code's output. - */ -print("I have learned the following:") -print("What features make Swift a modern and safe language") -print("How to use the Swift REPL in Terminal") -print("How to use playgrounds to make writing Swift fun and simple") -/*: - Now print your own phrases to the console. Pick one of your favorite songs. Use your knowledge of the `print` function to display the song title and artist. + ## Exercise - Constants + Declare a constant called `friends` to represent the number of friends you have on social media. Give it a value between 50 and 1000. Print out the value by referencing your constant. */ + let friends = 700 + print(friends) /*: - Use multiple `print` functions to write out some of the lyrics to the song. + Now assume you go through and remove friends that aren't active on social media. Attempt to update your `friends` constant to a lower number than it currently is. Observe what happens and then move to the next step. */ - - +//friends = 500 /*: + Does the above code compile? Why not? Print your explanation to the console using the `print` function. Go back and delete your line of code that updates the `friends` constant to a lower number so that the playground will compile properly. + */ + print("No, it won't compile because friends is a 'let' constant .. you cant changet that:)") - _Copyright © 2018 Apple Inc._ - - _Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:_ - - _The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software._ - _THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE._ - */ +//: page 1 of 10 | [Next: App Exercise - Step Goal](@next) From 78e6cda633caff10b3bbd78837122a9aec8915e1 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 03:13:15 +0200 Subject: [PATCH 02/29] Update Contents.swift --- .../Contents.swift | 50 +++---------------- 1 file changed, 8 insertions(+), 42 deletions(-) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Contents.swift index d86a552..4f29da4 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Contents.swift @@ -1,50 +1,16 @@ /*: - ## Exercise - Constants + ## App Exercise - Step Goal - Declare a constant called `friends` to represent the number of friends you have on social media. Give it a value between 50 and 1000. Print out the value by referencing your constant. - */ - - -/*: - Now assume you go through and remove a lot of your friends that aren't active on social media. Update your `friends` constant to a lower number than it currently is between 1 and 900. - */ - - -/*: - Does the above code compile? Why not? Print your explanation to the console using the `print` function. Go back and delete your line of code that updates the `friend` constant to a lower number so that the playground will compile properly. - */ - - -/*: - Declare a variable `age` and set it to your own age. Print `age` to the console. - */ - - -/*: - Now pretend you just had a birthday, and update the `age` variable accordingly. Print `age` to the console. + >These exercises reinforce Swift concepts in the context of a fitness tracking app. + Your fitness tracking app needs to know goal number of steps per day. Create a constant `goalSteps` and set it to 10000. */ +let goalSteps = 1000 /*: - - */ - - -/*: - Create a double variable with a value of 1.1. Update it to 2.2, 3.3, and 4.4. Print out the value after each assignment (again by referencing the variable you created). - */ - - -/*: - Create a Boolean variable and set it to `true`. Print the variable, then assign it a value of `false`, and print it again. - */ - - -/*: - Create two variables: one with a value of 0, the other with a value of 0.0. Try to assign the second variable to the first, and you'll receive an error. Add the necessary type annotation to allow the second variable to be assigned to the first. + Use two `print` functions to print two separate lines to the console. The first line should say "Your step goal for the day is:", and the second line should print the value of `goalSteps` by referencing your constant. */ + print ("You step goal for the day is:") + print (goalSteps) -/*: - Create a variable integer with a value of 1,000,000,000. Format it using commas, so it's easier to read. - */ - +//: [Previous](@previous) | page 2 of 10 | [Next: Exercise - Variables](@next) From 1e6899590d800b8bc845d26c8d56960979832eb9 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 03:14:06 +0200 Subject: [PATCH 03/29] Update Contents.swift --- .../1. Exercise - Constants.xcplaygroundpage/Contents.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift index 8721318..c152620 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/1. Exercise - Constants.xcplaygroundpage/Contents.swift @@ -1,18 +1,20 @@ /*: ## Exercise - Constants - Declare a constant called `friends` to represent the number of friends you have on social media. Give it a value between 50 and 1000. Print out the value by referencing your constant. */ + let friends = 700 + print(friends) /*: Now assume you go through and remove friends that aren't active on social media. Attempt to update your `friends` constant to a lower number than it currently is. Observe what happens and then move to the next step. */ - +//friends = 500 /*: Does the above code compile? Why not? Print your explanation to the console using the `print` function. Go back and delete your line of code that updates the `friends` constant to a lower number so that the playground will compile properly. */ + print("No, it won't compile because friends is a 'let' constant .. you cant changet that:)") //: page 1 of 10 | [Next: App Exercise - Step Goal](@next) From 7fca41ef9ea021a40163d9736a62171684b1e38a Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 03:14:55 +0200 Subject: [PATCH 04/29] Update Contents.swift --- .../Contents.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/2. App Exercise - Step Goal.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/2. App Exercise - Step Goal.xcplaygroundpage/Contents.swift index b71a821..4f29da4 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/2. App Exercise - Step Goal.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/2. App Exercise - Step Goal.xcplaygroundpage/Contents.swift @@ -2,14 +2,15 @@ ## App Exercise - Step Goal >These exercises reinforce Swift concepts in the context of a fitness tracking app. - Your fitness tracking app needs to know goal number of steps per day. Create a constant `goalSteps` and set it to 10000. */ - +let goalSteps = 1000 /*: Use two `print` functions to print two separate lines to the console. The first line should say "Your step goal for the day is:", and the second line should print the value of `goalSteps` by referencing your constant. */ + print ("You step goal for the day is:") + print (goalSteps) //: [Previous](@previous) | page 2 of 10 | [Next: Exercise - Variables](@next) From 1078fd1027c03c2da7f44d73cf5f0857d1172e6f Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 03:15:35 +0200 Subject: [PATCH 05/29] Update Contents.swift --- .../Contents.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/3. Exercise - Variables.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/3. Exercise - Variables.xcplaygroundpage/Contents.swift index 1b6fe9b..7f2f388 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/3. Exercise - Variables.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/3. Exercise - Variables.xcplaygroundpage/Contents.swift @@ -1,18 +1,18 @@ -/*: + /*: ## Exercise - Variables Declare a variable `schooling` and set it to the number of years of school that you have completed. Print `schooling` to the console. */ - - +var schooling = 12 +print(schooling) /*: Now imagine you just completed an additional year of school, and update the `schooling` variable accordingly. Print `schooling` to the console. */ - +schooling = schooling+1; print(schooling) /*: Does the above code compile? Why is this different than trying to update a constant? Print your explanation to the console using the `print` function. */ - +print("Variables are made to be changable as opposed to 'constant' declared using 'let' keyword: once declared contant cant be changed:) ") //: [Previous](@previous) | page 3 of 10 | [Next: App Exercise - Step Count](@next) From f7dcf232b2ce82fcf15b5faf29606bced7fe96de Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 03:16:02 +0200 Subject: [PATCH 06/29] Update Contents.swift --- .../Contents.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/4. App Exercise - Step Count.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/4. App Exercise - Step Count.xcplaygroundpage/Contents.swift index 4c95fb5..e6bd4f6 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/4. App Exercise - Step Count.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/4. App Exercise - Step Count.xcplaygroundpage/Contents.swift @@ -5,11 +5,15 @@ Create a variable called `steps` that will keep track of the number of steps you take throughout the day. Set its initial value to 0 to represent the step count first thing in the morning. Print `steps` to the console. */ - +var steps = 0 +print (steps) /*: Now assume the tracker has been keeping track of steps all morning, and you want to show the user the latest step count. Update `steps` to be 2000. Print `steps` to the console. Then print "Good job! You're well on your way to your daily goal." */ + steps = 2000 + print(steps) + print ("Good job! You're well on your way to your daily goal.") //: [Previous](@previous) | page 4 of 10 | [Next: Exercise - Constant or Variable?](@next) From 98ec16afe03dc96414abef03789d59ff555059a0 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 03:16:44 +0200 Subject: [PATCH 07/29] Update Contents.swift --- .../Contents.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/5. Exercise - Constant Or Variable.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/5. Exercise - Constant Or Variable.xcplaygroundpage/Contents.swift index 1d38a49..d5c89b4 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/5. Exercise - Constant Or Variable.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/5. Exercise - Constant Or Variable.xcplaygroundpage/Contents.swift @@ -10,7 +10,11 @@ For each of the metrics above, declare either a constant or a variable and assign it a value corresponding to a hypothetical post. Be sure to use proper naming conventions. */ - +var numberOfLikes = 1000 +var numberOfComments = 250 +let yearCreated = 2020 +let monthCreated = "June" +let dayCreated = 10 From 9da9455c1fd0e7c92e4705a600a0e1ca551caa0d Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 03:17:31 +0200 Subject: [PATCH 08/29] Update Contents.swift --- .../Contents.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/6. App Exercise - Constant or Variable.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/6. App Exercise - Constant or Variable.xcplaygroundpage/Contents.swift index a9bfc4b..cd6093d 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/6. App Exercise - Constant or Variable.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/6. App Exercise - Constant or Variable.xcplaygroundpage/Contents.swift @@ -12,8 +12,14 @@ - Average heart rate: The user's average heart rate over the last 24 hours */ - - +let userName = "Minar El-Aasser" +print("User name wont change") +var userAge = 34 +print("age changes every 12 months") +var goalNumberOfSteps = 300 +print("the goal should be adjustable as you practice more:)") +var averageHeartRate = 75 +print("your heart rate may vary due to your health condition in the last 24 hours from one day to another") /*: From 9961caa490252e30b4851374e4383980bbe0f4d9 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 03:18:05 +0200 Subject: [PATCH 09/29] Update Contents.swift --- .../Contents.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/7. Exercise - Types and Type Safety.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/7. Exercise - Types and Type Safety.xcplaygroundpage/Contents.swift index 6a26c32..d2a60c3 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/7. Exercise - Types and Type Safety.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/7. Exercise - Types and Type Safety.xcplaygroundpage/Contents.swift @@ -4,20 +4,37 @@ Declare two variables, one called `firstDecimal` and one called `secondDecimal`. Both should have decimal values. Look at both of their types by holding Option and clicking on the variable name. */ +var firstDecimal = 9.5 +var secondDecimal = 5.2 + /*: Declare a variable called `trueOrFalse` and give it a boolean value. Try to assign it to `firstDecimal` like so: `firstDecimal = trueOrFalse`. Does it compile? Print a statement to the console explaining why not, and remove the line of code that will not compile. */ +let trueOrFalse = true +//firstDecimal = trueOrFalse +print("This above commented command won't allow the code to compile because you cannot assign value of type 'Bool' to type 'Double'") + /*: Declare a variable and give it a string value. Then try to assign it to `firstDecimal`. Does it compile? Print a statement to the console explaining why not, and remove the line of code that will not compile. */ + var myString = "My String" + //firstDecimal = myString + print("This above commented command won't allow the code to compile because you cannot assign value of type 'String' to type 'Double'") + + /*: Finally, declare a variable with a whole number value. Then try to assign it to `firstDecimal`. Why won't this compile even though both variables are numbers? Print a statement to the console explaining why not, and remove the line of code that will not compile. */ + var wholeNumber = 10 + //firstDecimal = wholeNumber + print("This above commented command won't allow the code to compile because you cannot assign value of type 'Int' to type 'Double'") + + //: [Previous](@previous) | page 7 of 10 | [Next: App Exercise - Tracking Different Types](@next) From 0e98182509bd82f15c6e7de6bad84dfbb47fe690 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 03:18:45 +0200 Subject: [PATCH 10/29] Update Contents.swift --- .../Contents.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/8. App Exercise - Tracking Different Types.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/8. App Exercise - Tracking Different Types.xcplaygroundpage/Contents.swift index 1badf6b..19a1d82 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/8. App Exercise - Tracking Different Types.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/8. App Exercise - Tracking Different Types.xcplaygroundpage/Contents.swift @@ -5,11 +5,21 @@ You have declared a number of constants and variables to keep track of fitness information. Declare one more variable with a boolean value called `hasMetStepGoal`. */ - +let hasMetStepGoal = false /*: When you declared a constant for goal number of steps and a variable for current step count, you likely assigned each a value in the thousands. This can be difficult to read. Redeclare this constant and variable and, when assigning each a value in the thousands, format the number so that it is more readable. */ +//to make it more readable in the code +var numberOfStepsGoal = 1000_000 +//to make it more readable in the display +import Foundation +let numberFormatter = NumberFormatter() +numberFormatter.numberStyle = .decimal +var numberOfStepsGoalFormatted = numberFormatter.string(from: NSNumber(value:numberOfStepsGoal))! +print(numberOfStepsGoalFormatted) + //: [Previous](@previous) | page 8 of 10 | [Next: Exercise - Type Inference and Required Values](@next) + From 8820a31fb9a352b5eb8d2dd3282a6857aa0a589b Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 03:19:11 +0200 Subject: [PATCH 11/29] Update Contents.swift --- .../Contents.swift | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift index 5c81e84..48e7c9f 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/9. Exercise - Type Inference and Required Values.xcplaygroundpage/Contents.swift @@ -3,21 +3,22 @@ Declare a variable called `name` of type `String`, but do not give it a value. Print `name` to the console. Does the code compile? Remove any code that will not compile. */ - + var name: String + //print(name) /*: Now assign a value to `name`, and print it to the console. */ - - +name = "Minar" +print(name) /*: Declare a variable called `distanceTraveled` and set it to 0. Do not give it an explicit type. */ - +var distanceTraveled: Float /*: Now assign a value of 54.3 to `distanceTraveled`. Does the code compile? Go back and set an explicit type on `distanceTraveled` so the code will compile. */ - +distanceTraveled = 54.3 //: [Previous](@previous) | page 9 of 10 | [Next: App Exercise - Percent Completed](@next) From df8b0fb122fd055242918bedf4a51d9d00c5550f Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 03:20:43 +0200 Subject: [PATCH 12/29] Update Contents.swift --- .../Contents.swift | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/10. App Exercise - Percent Completed.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/10. App Exercise - Percent Completed.xcplaygroundpage/Contents.swift index 21166b7..3bbc75c 100644 --- a/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/10. App Exercise - Percent Completed.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/2 - Constants, Variables, and Data Types/lab/Lab - Constants and Variables.playground/Pages/10. App Exercise - Percent Completed.xcplaygroundpage/Contents.swift @@ -1,25 +1,24 @@ /*: ## App Exercise - Percent Completed - >These exercises reinforce Swift concepts in the context of a fitness tracking app. - You decide that your fitness tracking app should show the user what percentage of his/her goal has been achieved so far today. Declare a variable called `percentCompleted` and set it to 0. Do not explicity assign it a type. */ - +var percentCompleted: Double = 0 /*: Imagine that partway through the day a user has taken 3,467 steps out of the 10,000 step goal. This means he/she is 34.67% of the way to his/her goal. Assign 34.67 to `percentCompleted`. Does the code compile? Go back and explicity assign a type to `percentCompleted` that will allow the code to compile. */ + var stepGoal = 10_000 + var stepsTaken = 3467 +percentCompleted = (100.0 * Double(stepsTaken)) / Double(stepGoal) +print((percentCompleted),"%") -/*: - - _Copyright © 2018 Apple Inc._ +/*: + _Copyright © 2018 Apple Inc._ _Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:_ - _The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software._ - _THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE._ */ //: [Previous](@previous) | page 10 of 10 From 03d34cd5da6249026dac743ae8dccf40728263da Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 04:07:55 +0200 Subject: [PATCH 13/29] Update Contents.swift --- .../Contents.swift | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift index 4cbbad4..001fd5b 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/1. Exercise - Basic Arithmetic.xcplaygroundpage/Contents.swift @@ -3,26 +3,37 @@ You decide to build a shed and want to know beforehand the area of your yard that it will take up. Create two constants, `width` and `height`, with values of 10 and 20, respectively. Create an `area` constant that is the result of multiplying the two previous constants together, and print out the result. */ - + let width = 10 + let height = 20 + let area = width * height + print(area) /*: You decide that you'll divide your shed into two rooms. You want to know if dividing it equally will leave enough room for some of your larger storage items. Create a `roomArea` constant that is the result of dividing `area` in half. Print out the result. */ + let roomArea = area / 2; print(roomArea) /*: Create a `perimeter` constant whose value equals `width` plus `width` plus `height` plus `height`, then print out the result. */ + let perimeter = width + width + height + height; print(perimeter) /*: Print what you would expect the result of integer division of 10 divided by 3 to be. Create a constant, `integerDivisionResult` that is the result of 10 divided by 3, and print the value. */ - +let integerDivisionResult = 10 / 3 +print(integerDivisionResult) /*: Now create two constants, `double10` and `double3`, set to 10 and 3, and declare their types as `Double` values. Declare a final constant `divisionResult` equal to the result of `double10` divided by `double3`. Print the value of `divisionResult`. How does this differ from the value when using integer division? */ + let double10:Double = 10 + let double3:Double = 3 + let divisionResult = double10 / double3 + print(divisionResult) + /*: @@ -33,6 +44,11 @@ *circumference = 2 * pi * radius.* */ let pi = 3.1415927 +let radius = 5.0 +let diameter = 2 * radius +let circumference = 2 * pi * radius +print("diameter: ", diameter) +print("circumference: ", circumference) //: page 1 of 8 | [Next: App Exercise - Fitness Calculations](@next) From 2ede7a0d68a3dd55568f39fd2afc49c436a13c62 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 04:21:03 +0200 Subject: [PATCH 14/29] Update Contents.swift --- .../Contents.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/2. App Exercise - Fitness Calculations.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/2. App Exercise - Fitness Calculations.xcplaygroundpage/Contents.swift index 7384367..44d2a47 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/2. App Exercise - Fitness Calculations.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/2. App Exercise - Fitness Calculations.xcplaygroundpage/Contents.swift @@ -5,16 +5,35 @@ Your fitness tracker keeps track of users' heart rate, but you might also want to display their average heart rate over the last hour. Create three constants, `heartRate1`, `heartRate2`, and `heartRate3`. Give each constant a different value between 60 and 100. Create a constant `addedHR` equal to the sum of all three heart rates. Now create a constant called `averageHR` that equals `addedHR` divided by 3 to get the average. Print the result. */ + let heartRate1 = 62 + let heartRate2 = 80 + let heartRate3 = 98 + let addedHR = heartRate1 + heartRate2 + heartRate3 + let averageHR = addedHR / 3 + print("addedHR: ", addedHR) + print("averageHR: ", averageHR) /*: Now create three more constants, `heartRate1D`, `heartRate2D`, and `heartRate3D`, equal to the same values as `heartRate1`, `heartRate2`, and `heartRate3`. These new constants should be of type `Double`. Create a constant `addedHRD` equal to the sum of all three heart rates. Create a constant called `averageHRD` that equals the `addedHRD` divided by 3 to get the average of your new heart rate constants. Print the result. Does this differ from your previous average? Why or why not? */ + let heartRate1D = Double(heartRate1) + let heartRate2D = Double(heartRate2) + let heartRate3D = Double(heartRate3) + print("heartRate1D: ", heartRate1D) + print("heartRate2D: ", heartRate2D) + print("heartRate3D: ", heartRate3D) + /*: Imagine that partway through the day a user has taken 3,467 steps out of the 10,000 step goal. Create constants `steps` and `goal`. Both will need to be of type `Double` so that you can perform accurate calculations. `steps` should be assigned the value 3,467, and `goal` should be assigned 10,000. Create a constant `percentOfGoal` that equals an expression that evaluates to the percent of the goal that has been achieved so far. */ +let steps, goal :Double +steps = 3467 +goal = 10000 +let percentOfGoal = steps * 100 / goal +print("percentOfGoal:", percentOfGoal, "%") //: [Previous](@previous) | page 2 of 8 | [Next: Exercise - Compound Assignment](@next) From 7d05e3f9638f88c7a09ff3fb2019f46a6a4b402c Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 04:30:28 +0200 Subject: [PATCH 15/29] Update Contents.swift --- .../Contents.swift | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/3. Exercise - Compound Assignment.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/3. Exercise - Compound Assignment.xcplaygroundpage/Contents.swift index b0c97b5..dcf8178 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/3. Exercise - Compound Assignment.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/3. Exercise - Compound Assignment.xcplaygroundpage/Contents.swift @@ -3,6 +3,9 @@ Declare a variable whose value begins at 10. Using addition, update the value to 15 using the compound assignment operator. Using multiplication, update the value to 30 using compound assignment. Print out the variable's value after each assignment. */ +var myVar = 10 ;print("myVar: ", myVar) +myVar += 5 ;print("myVar: ", myVar) +myVar *= 2 ;print("myVar: ", myVar) /*: @@ -16,7 +19,17 @@ Print the balance of your piggy bank after each step. */ - +var piggyBank = 0 +piggyBank += 10 +print("piggyBank: ", piggyBank) +piggyBank += 20 +print("piggyBank: ", piggyBank) +piggyBank /= 2 +print("piggyBank: ", piggyBank) +piggyBank *= 3 +print("piggyBank: ", piggyBank) +piggyBank -= 3 +print("piggyBank: ", piggyBank) From a43649c4e92f16b43d920286a27aaac22f06813e Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 04:35:24 +0200 Subject: [PATCH 16/29] Update Contents.swift --- .../Contents.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/4. App Exercise - Counting.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/4. App Exercise - Counting.xcplaygroundpage/Contents.swift index 881654a..fdbbf6b 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/4. App Exercise - Counting.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/4. App Exercise - Counting.xcplaygroundpage/Contents.swift @@ -5,13 +5,17 @@ The most basic feature of your fitness tracking app is counting steps. Create a variable `steps` and set it equal to 0. Then increment its value by 1 to simulate a user taking a step. */ - +var steps = 0 +steps += 1 /*: In addition to tracking steps, your fitness tracking app tracks distance traveled. Create a variable `distance` of type `Double` and set it equal to 50. This will represent the user having traveled 50 feet. You decide, however, to display the distance in meters. 1 meter is approximately equal to 3 feet. Use a compound assignment operator to convert `distance` to meters. Print the result. */ + var distance : Double = 50 + distance /= 3 + print("distance: ", distance) //: [Previous](@previous) | page 4 of 8 | [Next: Exercise - Order of Operations](@next) From dc2e0e370e5f87d55b65bacf8f269f49e33133d6 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 04:39:56 +0200 Subject: [PATCH 17/29] Update Contents.swift --- .../Contents.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/5. Exercise - Order of Operations.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/5. Exercise - Order of Operations.xcplaygroundpage/Contents.swift index e8378a0..49e224a 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/5. Exercise - Order of Operations.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/5. Exercise - Order of Operations.xcplaygroundpage/Contents.swift @@ -1,23 +1,27 @@ /*: ## Exercise - Order of Operations - Print out what you think 10 + 2 * 5 evaluates to. Then print out the actual expression (i.e. `print(10 + 2 * 5)`) */ - +print("what i think: 20") +print("actual expression:", 10 + 2 * 5) /*: In a separate `print` statement, add in the necessary parentheses so that addition takes place before multiplication. */ + + print("updating expression:", (10 + 2) * 5) /*: Print out what you think 4 * 9 - 6 / 2 evaluates to. Then print out the actual expression. */ - +print("what i think: 33") +print("actual expression:", 4 * 9 - 6 / 2) /*: In a separate `print` statement, add in the necessary parentheses so that the subtraction is prioritized over the multiplication and division. */ + print("updating expression:", 4 * (9 - 6) / 2) //: [Previous](@previous) | page 5 of 8 | [Next: App Exercise - Complex Fitness Calculations](@next) From 211c735f89e8fc930b87a13c589b0864023c2cc7 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 04:48:09 +0200 Subject: [PATCH 18/29] Update Contents.swift --- .../Contents.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/6. App Exercise - Complex Fitness Calculations.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/6. App Exercise - Complex Fitness Calculations.xcplaygroundpage/Contents.swift index 1f2189c..319c2c4 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/6. App Exercise - Complex Fitness Calculations.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/6. App Exercise - Complex Fitness Calculations.xcplaygroundpage/Contents.swift @@ -5,6 +5,10 @@ If you completed the Fitness Calculations exercise, you calculated an average heart rate to display to the user. However, using proper order of operations you can do this in fewer steps. Create three separate heart rate constants, all of type `Double`, with values between 60 and 100. Then create a constant equal to the average heart rate. If you use correct order of operations you can do the heart calculation in one line. */ + + let heartRate1 = 62.0 , heartRate2 = 80.0 , heartRate3 = 98.0 + let averageHR = (heartRate1 + heartRate2 + heartRate3) / 3 + print("averageHR: ", averageHR) /*: @@ -12,6 +16,9 @@ You may want to also show the temperature in celsius. You can convert fahrenheit to celsius by taking `tempInFahrenheit` and subtracting 32, then multiplying the result by (5.0/9.0). Create a constant `tempInCelsius` that calculates in one line the temperature in celsius. */ + let tempInFahrenheit = 98.6 +let tempInCelsius = (tempInFahrenheit - 32)*(5.0/9.0) + print("Body temp. in celsius: ", tempInCelsius) //: [Previous](@previous) | page 6 of 8 | [Next: Exercise - Numeric Type Conversion](@next) From b2b2b072ed0b2b67027bac46285bc2e0a3b8fbc7 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 04:53:37 +0200 Subject: [PATCH 19/29] Update Contents.swift --- .../Contents.swift | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/7. Exercise - Numeric Type Conversion.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/7. Exercise - Numeric Type Conversion.xcplaygroundpage/Contents.swift index 93698b8..7af25a0 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/7. Exercise - Numeric Type Conversion.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/7. Exercise - Numeric Type Conversion.xcplaygroundpage/Contents.swift @@ -1,18 +1,23 @@ /*: ## Exercise - Numeric Type Conversion - Create an integer constant `x` with a value of 10, and a double constant `y` with a value of 3.2. Create a constant `multipliedAsIntegers` equal to `x` times `y`. Does this compile? If not, fix it by converting your `Double` to an `Int` in the mathematical expression. Print the result. */ + let x = 10 + let y = 3.2 + let multipliedAsIntegers = x * Int(y) + print("multipliedAsIntegers: ", multipliedAsIntegers) /*: Create a constant `multipliedAsDoubles` equal to `x` times `y`, but this time convert the `Int` to a `Double` in the expression. Print the result. */ + let multipliedAsDoubles = Double(x) * y + print("multipliedAsDoubles: ", multipliedAsDoubles) /*: Are the values of `multipliedAsIntegers` and `multipliedAsDoubles` different? Print a statement to the console explaining why. */ - +print("No, they are not the same. when converting a double to integer you lose precision. However, Int can be converted without loss of precision to a Double") //: [Previous](@previous) | page 7 of 8 | [Next: App Exercise - Converting Types](@next) From d4ad7f017f243824ccfa335594071d5bd5a03ebe Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 04:58:01 +0200 Subject: [PATCH 20/29] Update Contents.swift --- .../Contents.swift | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/8. App Exercise - Fitness Conversions.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/8. App Exercise - Fitness Conversions.xcplaygroundpage/Contents.swift index 5de2f53..8e9f726 100644 --- a/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/8. App Exercise - Fitness Conversions.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/3 - Operators/lab/Lab - Operators.playground/Pages/8. App Exercise - Fitness Conversions.xcplaygroundpage/Contents.swift @@ -1,22 +1,19 @@ /*: ## App Exercise - Converting Types - >These exercises reinforce Swift concepts in the context of a fitness tracking app. - If you completed the Fitness Calculations exercise, you calculated the percent of the daily step goal that a user has achieved. However, you did this by having `steps` be of type `Double`. But you can't really track a partial step, so `steps` should probably be of type `Int`. Go ahead and declare `steps` as type `Int` and give it a value between 500 and 6,000. Then declare `goal` as type `Int` and set it equal to 10,000. - Now create a constant `percentOfGoal` of type `Double` that equals the percent of the goal that has been reached so far. You'll need to convert your constants of type `Int` to be of type `Double` in your calculation. */ - - +var steps, goal : Int +steps = 3000 +goal = 10000 +let percentOfGoal : Double +percentOfGoal = Double(steps) * 100 / Double(goal) +print("percentOfGoal: ", percentOfGoal ) /*: - _Copyright © 2018 Apple Inc._ - _Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:_ - _The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software._ - _THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE._ */ //: [Previous](@previous) | page 8 of 8 From 01a4a16cd329d1954c11760fec2575ea6ca17b57 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 05:49:38 +0200 Subject: [PATCH 21/29] Update Contents.swift --- .../Contents.swift | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift index 5bb5325..9e2e9b6 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/2. Exercise - If and If-Else Statements.xcplaygroundpage/Contents.swift @@ -4,18 +4,35 @@ Imagine you're creating a machine that will count your money for you and tell you how wealthy you are based on how much money you have. A variable `dollars` has been given to you with a value of 0. Write an if statement that prints "Sorry, kid. You're broke!" if `dollars` has a value of 0. Observe what is printed to the console. */ var dollars = 0 +if dollars == 0{ +print("Sorry, kid. You're broke!") } /*: `dollars` has been updated below to have a value of 10. Write an an if-else statement that prints "Sorry, kid. You're broke!" if `dollars` has a value of 0, but prints "You've got some spending money!" otherwise. Observe what is printed to the console. */ dollars = 10 +if dollars == 0{ +print("Sorry, kid. You're broke!") } +else +{ + print("You've got some spending money!") +} /*: `dollars` has been updated below to have a value of 105. Write an an if-else-if statement that prints "Sorry, kid. You're broke!" if `dollars` has a value of 0, prints "You've got some spending money!" if `dollars` is less than 100, and prints "Looks to me like you're rich!" otherwise. Observe what is printed to the console. */ dollars = 105 - +if dollars == 0{ +print("Sorry, kid. You're broke!") } +else if dollars < 100 +{ + print("You've got some spending money!") +} +else +{ + print("Looks to me like you're rich!") +} //: [Previous](@previous) | page 2 of 9 | [Next: App Exercise - Fitness Decisions](@next) From e90e7a0dc8ee4bfcf1bb56b559e88f49dcb4e074 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 05:59:50 +0200 Subject: [PATCH 22/29] Update Contents.swift --- .../Contents.swift | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift index d0d23b3..895d9e2 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/3. App Exercise - Fitness Decisions.xcplaygroundpage/Contents.swift @@ -5,11 +5,30 @@ You want your fitness tracking app to give as much encouragement as possible to your users. Create a variable `steps` equal to the number of steps you guess you've taken today. Create a constant `stepGoal` equal to 10,000. Write an if-else statement that will print "You're almost halfway there!" if `steps` is less than half of `stepGoal`, and will print "You're over halfway there!" if `steps` is greater than half of `stepGoal`. */ - +var steps = 7000 +let stepGoal = 10000 +if (steps < stepGoal/2) +{ + print("You're almost halfway there!") +} +else +{ + print("You're over halfway there!") +} /*: Now create a new, but similar, if-else-if statement that prints "Way to get a good start today!" if `steps` is less than a tenth of `stepGoal`, prints "You're almost halfway there!" if `steps` is less than half of `stepGoal`, and prints "You're over halfway there!" if `steps` is greater than half of `stepGoal`. */ - - +if (steps < stepGoal/10) +{ + print("Way to get a good start today!") +} +else if (steps < stepGoal/2) +{ + print("You're almost halfway there!") +} +else +{ + print("You're over halfway there!") +} //: [Previous](@previous) | page 3 of 9 | [Next: Exercise - Boolean Practice](@next) From 5d52c6f53a2cd984fb9aa9297b271b61423f8aff Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 06:08:57 +0200 Subject: [PATCH 23/29] Update Contents.swift --- .../Contents.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift index 5db9740..049e6e6 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/4. Exercise - Boolean Practice.xcplaygroundpage/Contents.swift @@ -13,6 +13,15 @@ let hasFish = true let hasPizza = false let hasVegan = true +if ((hasFish || hasPizza) && hasVegan) +{ + print("Let's go!") +} +else +{ + print("Sorry, we'll have to think of somewhere else.") +} + /*: Imagine you're trying to decide whether or not to go on a walk. You decide that you'll go on a walk if it's not raining or if it's 82 degress or warmer and sunny out. Create a constant `isNiceWeather` that is equal to an expression that evaluates to a boolean indicating whether or not the weather is nice enough for you to go for a walk. Write an if statement that will print "I'm going for a walk!" if the weather is nice. @@ -21,5 +30,9 @@ let temp = 82 let isRaining = true let isSunny = true +let isNiceWeather = (!isRaining || temp>=82 && isSunny) + +if isNiceWeather { +print("I'm going for a walk!")} //: [Previous](@previous) | page 4 of 9 | [Next: App Exercise - Target Heart Rate](@next) From 574fb22e10330134f10edc649c9d9a6c87de2509 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 06:17:20 +0200 Subject: [PATCH 24/29] Update Contents.swift --- .../Contents.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift index 8740e75..06ee236 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift @@ -1,3 +1,4 @@ + /*: ## App Exercise - Target Heart Rate @@ -11,5 +12,21 @@ let targetLowerBound = 120 let targetUpperBound = 150 let currentHR = 147 +let isInTarget = targetLowerBound < currentHR && currentHR < targetUpperBound +let isBelowTarget = targetLowerBound > currentHR +let isAboveTarget = currentHR > targetUpperBound + +if isInTarget +{ + print("You're right on track!") +} else if isBelowTarget +{ + print("You're doing great, but try to push it a bit!") +} +else +{ + print("You're on fire! Slow it down just a bit.") +} + //: [Previous](@previous) | page 5 of 9 | [Next: Exercise - Switch Statements](@next) From 129a56e085304c2b0b4ac1110ac11184d9937de0 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 06:17:40 +0200 Subject: [PATCH 25/29] Update Contents.swift --- .../Contents.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift index 06ee236..d2c5125 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/5. App Exercise - Target Heart Rate.xcplaygroundpage/Contents.swift @@ -1,5 +1,4 @@ - -/*: + /*: ## App Exercise - Target Heart Rate >These exercises reinforce Swift concepts in the context of a fitness tracking app. From 530457fae825a8fa00f904db7ed6db3359a1ed31 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 06:23:28 +0200 Subject: [PATCH 26/29] Update Contents.swift --- .../Contents.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift index ed3e397..975d2b7 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/6. Switch Statements.xcplaygroundpage/Contents.swift @@ -3,11 +3,21 @@ Imagine you're on a baseball team nearing the end of the season. Create a `leaguePosition` constant with a value of 1. Using a `switch` statement, print "Champions!" if the `leaguePosition` is 1, "Runners up" if the value is 2, "Third place" if the value is 3, and "Bad season!" in all other cases. */ - +let leaguePosition = 1 +switch leaguePosition { + case 1: print("Champions!") + case 2: print("Runners up") + case 3: print("Third place") + default: print("Bad season!") +} /*: Write a new `switch` statement that prints "Medal winner" if `leaguePosition` is within the range of 1-3. Otherwise, print "No medal awarded". */ + switch leaguePosition { + case 1...3: print("Medal winner") + default: print("No medal awarded") + } //: [Previous](@previous) | page 6 of 9 | [Next: App Exercise - Heart Rate Zones](@next) From 625afdbd82a4851f9c9f99664783de41dd87e6d4 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 06:30:16 +0200 Subject: [PATCH 27/29] Update Contents.swift --- .../Contents.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/7. App Exercise - Heart Rate Zones.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/7. App Exercise - Heart Rate Zones.xcplaygroundpage/Contents.swift index 40f36d4..57b0f7d 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/7. App Exercise - Heart Rate Zones.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/7. App Exercise - Heart Rate Zones.xcplaygroundpage/Contents.swift @@ -16,6 +16,15 @@ If `currentHR` is above the listed zones, print some kind of warning asking the user to slow down. */ let currentHR = 128 +switch currentHR { + case 100...120: print("You are in the Very Light zone. Activity in this zone helps with recovery.") + case 121...140: print("You are in the Light zone. Activity in this zone helps improve basice endurance and fat burning.") + case 141...160: print("You are in the Moderate zone. Activity in this zone helps improve aerobic fitness.") + case 161...180: print("You are in the Hard zone. Activity in this zone increases maximum performance capacity for shorter sessions.") + case 181...200: print("You are in the Maximum zone. Activity in this zone helps fit athletes develop speed.") + case 201...: print("Warning! You need to slow down") + default: print("Please check your current HR") +} //: [Previous](@previous) | page 7 of 9 | [Next: Exercise - Ternary Operator](@next) From 6688a6c536c08e739b5fdfccf007a1226426ff35 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 06:39:43 +0200 Subject: [PATCH 28/29] Update Contents.swift --- .../Contents.swift | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift index 24cbb56..0b1376e 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/8. Exercise - Ternary Operator.xcplaygroundpage/Contents.swift @@ -6,11 +6,14 @@ let number1 = 14 let number2 = 25 -var largest: Int -if number1 > number2 { - largest = number1 -} else { - largest = number2 -} +var largest: Int = 0 +// if number1 > number2 { +// largest = number1 +// } else { +// largest = number2 +// } + +(number1 > number2) ? (largest = number1) : (largest = number1) +print("The largest number is: ",largest) //: [Previous](@previous) | page 8 of 9 | [Next: App Exercise - Ternary Messages](@next) From 005862d24c0eaf8acf3401a551c23c1f2118b937 Mon Sep 17 00:00:00 2001 From: minarelaasser <33254588+minarelaasser@users.noreply.github.com> Date: Wed, 10 Jun 2020 06:42:21 +0200 Subject: [PATCH 29/29] Update Contents.swift --- .../Contents.swift | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift index 5c86dde..2e2ab48 100644 --- a/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift +++ b/Student Resources/1 - Getting Started/4 - Control Flow/lab/Lab - Control Flow.playground/Pages/9. App Exercise - Ternary Messages.xcplaygroundpage/Contents.swift @@ -1,28 +1,18 @@ /*: ## App Exercise - Ternary Messages - >These exercises reinforce Swift concepts in the context of a fitness tracking app. - The code below should look similar to code you wrote in the Fitness Decisions exercise. The if-else statement is actually unnecessary, and instead you can print either one statement or the other all on one line using the ternary operator. Go ahead and refactor the code below to do just that. */ let stepGoal = 10000 let steps = 3948 -if steps < stepGoal / 2 { - print("Almost halfway!") -} else { - print("Over halfway!") -} +steps < stepGoal / 2 ? print("Almost halfway!") : print("Over halfway!") /*: - _Copyright © 2018 Apple Inc._ - _Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:_ - _The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software._ - _THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE._ */ //: [Previous](@previous) | page 9 of 9