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 @@ -10,14 +10,15 @@ 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.
*/


print("Title | Alive")
print("Artist | David Guetta feat. Sia ")
/*:
Use multiple `print` functions to write out some of the lyrics to the song.
*/



print("I'm bulletproof nothing to lose")
print("Ricochet, you take your aim")
print("You shoot me down but I won't fall")
print("I am titanium")
/*:

_Copyright © 2018 Apple Inc._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*:
## 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 = 50
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.
*/


// Cannot assign to value: 'friends' is a 'let' constant
//you can't change a costant.
/*:
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.
*/


//No
print("compile error occurs because 'friends' is a 'let' which is constant value, if you are willing to change it, it supposed to be 'var' ")
//: page 1 of 10 | [Next: App Exercise - Step Goal](@next)
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*:
## 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.
*/

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 = 16
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("yes, the variable can be changed if you want while the constant can't ")

//: [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 @@ -2,17 +2,17 @@
## Exercise - Constant or Variable?

Imagine you're creating a simple photo sharing app. You want to keep track of the following metrics for each post:
- Number of likes: the number of likes that a photo has received
- Number of comments: the number of comments other users have left on the photo
- Year created: The year the post was created
- Month created: The month the post was created represented by a number between 1 and 12
- Day created: The day of the month the post was created
- Number of likes: the number of likes that a photo has received
- Number of comments: the number of comments other users have left on the photo
- Year created: The year the post was created
- Month created: The month the post was created represented by a number between 1 and 12
- Day created: The day of the month the post was created

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 = 44
var numberOfComments = 17
let yearCreated = 2020
let monthCreated = 6
let dayCreated = 6
//: [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 name = "Norah"
print("Name won't be changed so I declared it as a constant")
var age = 21
print("Age is gonna change every year so I declared it as a variable")
var numberOfStepsToday = 5000
print("Steps are going to change everyday so I declared it as a variable")
let goalNumberofSteps = 1200
print("The goal number of steps won't be changed so I declared it as a constant")
var averageHeartRate = 58
print("The average heart rate is gonna be updated so I declared it as a variable")
/*:
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,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 = 11.1 // Double
var secondDecimal = 111.11 // Double
/*:
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("there is a mismatch between the types, 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 string = ""
//firstDecimal = string
print("there is a mismatch between the types, 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.
*/


//: [Previous](@previous) | page 7 of 10 | [Next: App Exercise - Tracking Different Types](@next)
var number = 5
//firstDecimal = number
print("Even though they are all numbers but number is an Int and firstDecimal is a Double and you cannot assign value of type 'Int' to type 'Double'")
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

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 = true

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


var numberOfStepsToday = 5_000
let goalNumberofSteps = 1_2000
//: [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,21 @@

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 = "Norah"
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 @@ -3,27 +3,33 @@

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


print(3)
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)

/*:
Given the value pi (3.1415927), create a `radius` constant with a value of 5.0, then calculate the diameter and circumference of the circle using the following equations, and print the results:
Expand All @@ -33,6 +39,10 @@
*circumference = 2 * pi * radius.*
*/
let pi = 3.1415927

let radius = 5.0
let diameter = 2 * radius
let circumference = 2 * pi * radius
print(diameter)
print(circumference)

//: page 1 of 8 | [Next: App Exercise - Fitness Calculations](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@

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 = 61
let heartRate2 = 87
let heartRate3 = 99
let addedHR = heartRate1 + heartRate2 + heartRate3
let averageHR = addedHR / 3
print(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 = 61
let heartRate2D: Double = 87
let heartRate3D: Double = 99
let addedHRD = heartRate1D + heartRate2D + heartRate3D
let averageHRD = addedHRD / 3
print(averageHRD)
// yes, because now its a Double and the decimal points will occure, while if we didn't specify the type it would be Int which means no decimal points, and now its more accurte.

/*:
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: Double = 3467
let goal: Double = 10000
let percentOfGoal = steps/goal * 100

//: [Previous](@previous) | page 2 of 8 | [Next: Exercise - Compound Assignment](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

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 start = 10
print(start)
start += 5
print(start)
start *= 2
print(start)
/*:
Create a variable called `piggyBank` that begins at 0. You will use this to keep track of money you earn and spend. For each point below, use the right compound assignment operator to update the balance in your piggy bank.

Expand All @@ -16,9 +20,16 @@

Print the balance of your piggy bank after each step.
*/





var piggyBank = 0
print(piggyBank)
piggyBank += 10
print(piggyBank)
piggyBank += 20
print(piggyBank)
piggyBank /= 2
print(piggyBank)
piggyBank *= 3
print(piggyBank)
piggyBank -= 3
print(piggyBank)
//: [Previous](@previous) | page 3 of 8 | [Next: App Exercise - Counting](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

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
//: [Previous](@previous) | page 4 of 8 | [Next: Exercise - Order of Operations](@next)
Loading