Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

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 = 200
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.
*/
Expand All @@ -13,6 +13,6 @@
/*:
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(friends)

//: page 1 of 10 | [Next: App Exercise - Step Goal](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

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.
*/

percentCompleted = 34.67

/*:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

Your fitness tracking app needs to know goal number of steps per day. Create a constant `goalSteps` and set it to 10000.
*/

let goalSteps = 10000

/*:
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("Your step goal for the day is:")
print(goalSteps)
//: [Previous](@previous) | page 2 of 10 | [Next: Exercise - Variables](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

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+=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("Constants cannot change in value but variables can be changed")

//: [Previous](@previous) | page 3 of 10 | [Next: App Exercise - Step Count](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

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)
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

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 likes=101
var comments=12
let yearCreated=2021
let monthCreated=4
let dayCreated=11
//: [Previous](@previous) | page 5 of 10 | [Next: App Exercise - Fitness Tracker: Constant or Variable?](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
- Goal number of steps: The user's goal for number of steps to take each day
- Average heart rate: The user's average heart rate over the last 24 hours
*/





let username="User1"
print("The name will never change")
var age=19
print("Age changes every year")
var stepsToday=2100
print("Steps taken every day changes")
let goalSteps = 10000
print("The Goal Steps don't change")
var averageHeartRate=75
print("Heart Rate average changes with moving")
/*:
Now go back and add a line after each constant or variable declaration. On those lines, print a statement explaining why you chose to declare the piece of information as a constant or variable.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@

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:Double=0.0
var secondDecimal:Double=1.0

/*:
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.
*/


var trueOrFalse:Bool = true
print("Type Safety prevents assigning incompatible data types")
/*:
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 str:String = "Hello"
print("Cannot assign string to 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 whole:Int = 13
print("Cannot assign integer to double")
//: [Previous](@previous) | page 7 of 10 | [Next: App Exercise - Tracking Different Types](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

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`.
*/

var hasMetStepGoal:Bool

/*:
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.
*/
let goalSteps=2000
var stepsTaken = 1500



//: [Previous](@previous) | page 8 of 10 | [Next: Exercise - Type Inference and Required Values](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -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


/*:
Now assign a value to `name`, and print it to the console.
*/


name="Mohamed"
print(name)
/*:
Declare a variable called `distanceTraveled` and set it to 0. Do not give it an explicit type.
*/

var distanceTraveled:Double = 0

/*:
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)
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,46 @@

1. `9 == 9`
*/


true
print(9==9)
/*:
2. `9 != 9`
*/


false
print(9 != 9)
/*:
3. `47 > 90`
*/


false
print(47>90)
/*:
4. `47 < 90`
*/


true
print(47<90)
/*:
5. `4 <= 4`
*/


true
print(4<=4)
/*:
6. `4 >= 5`
*/


false
print(4>=5)
/*:
7. `(47 > 90) && (47 < 90)`
*/


false
print(47>90 && 47<90)
/*:
8. `(47 > 90) || (47 < 90)`
*/


true
print(47>90 || 47<90)
/*:
9. `!true`
*/


false
print(!true)
//: page 1 of 9 | [Next: Exercise - If and If-Else Statements](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,36 @@
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>0 && dollars<100){
print("You've got some spending money!")
}
else{
print("Looks to me you're rich!")
}

//: [Previous](@previous) | page 2 of 9 | [Next: App Exercise - Fitness Decisions](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@

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=100
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 && steps>stepGoal/10){
print("You're almost halfway there!")
}
else{
print("You're over halfway there!")
}

//: [Previous](@previous) | page 3 of 9 | [Next: Exercise - Boolean Practice](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@
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.
*/
let temp = 82
let isRaining = true
let isSunny = true

let isNiceWeather = !isRaining && isSunny || temp>=82
if(isNiceWeather){
print("I'm going for a walk!")
}

//: [Previous](@previous) | page 4 of 9 | [Next: App Exercise - Target Heart Rate](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
let targetLowerBound = 120
let targetUpperBound = 150
let currentHR = 147

let isInTarget:Bool,isBelowTarget:Bool,isAboveTarget:Bool
isInTarget = currentHR>targetLowerBound && currentHR<targetUpperBound
isBelowTarget = currentHR<targetLowerBound
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)
Loading