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
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

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 = 500
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 = 400
print(" cannot modify a constant")
/*:
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.
*/
Expand Down
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 = 18
print(schooling)
/*:
Now imagine you just completed an additional year of school, and update the `schooling` variable accordingly. Print `schooling` to the console.
*/


schooling = 19
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("you can't modify a constant in lifetime of the program ")

//: [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,12 @@

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_number = 50
var comments = 12
let year_created = 2020
let month_created = 10
let day_created = 3
//: [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,8 +11,8 @@
- 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 user_name = "hajar"
let user_age = 29



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@

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 = 10
var secondDecimal = 20
/*:
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 = true
//firstDecimal = trueOrFalse
print("you can't assign a viariable type boolean to int ")
/*:
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 myword = "hajar"
//firstDecimal = myword
print("you can't assign a viariable type boolean to int ")
/*:
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.
*/

print("i didn't understand what's u mean by whole number ")

//: [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 @@ -6,10 +6,10 @@
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 = 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.
*/


let goal_setps = 10_000
var current_steps = 5_000
//: [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
//print(name)
/*:
Now assign a value to `name`, and print it to the console.
*/

name = "bijo"
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,48 @@

1. `9 == 9`
*/


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


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


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


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


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


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


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

print(true)
print((47 > 90) || (47 < 90))

/*:
9. `!true`
*/


print(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,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!")
}else
{
print("You've got some spending money!")
}

/*:
`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)
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@

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 = 2_219
let stepGoal = 10_000
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)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ let hasFish = true
let hasPizza = false
let hasVegan = true

if(hasVegan == true && (hasFish == true || hasPizza == true))
{
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.
Expand All @@ -21,5 +28,14 @@ let temp = 82
let isRaining = true
let isSunny = true

var isNiceWeather = false

if temp == 82 && !isRaining && isSunny
{
isNiceWeather = true
}
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 @@ -11,5 +11,20 @@ let targetLowerBound = 120
let targetUpperBound = 150
let currentHR = 147

let isInTarget = (currentHR > targetLowerBound) && (currentHR < targetUpperBound)
let isBelowTarget = currentHR < targetLowerBound
var isAboveTarget = currentHR > targetUpperBound

if(isInTarget)
{
print("You're right on track!")
}
if(isBelowTarget)
{
print("You're doing great, but try to push it a bit!")
}
if(isAboveTarget)
{
print("You're on fire! Slow it down just a bit.")
}
//: [Previous](@previous) | page 5 of 9 | [Next: Exercise - Switch Statements](@next)
Loading