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 @@ -12,11 +12,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("Sway, Michael Buble")


/*:
Use multiple `print` functions to write out some of the lyrics to the song.
*/

print("When marimba rhythms start to play")
print("Dance with me, make me sway")
print("Like a lazy ocean hugs the shore")
print("Hold me close, sway me more")

//:page 14 of 16 | [Next: Exercise: Go! Fight! Win!](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ 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("Sway, Michael Buble")


/*:
Use multiple `print` functions to write out some of the lyrics to the song.
*/


print("When marimba rhythms start to play")
print("Dance with me, make me sway")
print("Like a lazy ocean hugs the shore")
print("Hold me close, sway me more")

/*:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@

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

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

//friends = 50

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

print("You can not change a value of a constant, switch to var to make it mutable")

/*:
Declare a variable `age` and set it to your own age. Print `age` to the console.
*/

var age = 26
print(age)

/*:
Now pretend you just had a birthday, and update the `age` variable accordingly. Print `age` to the console.
*/

age = 27
print(age)
/*:

*/
Expand All @@ -32,19 +35,108 @@
/*:
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).
*/

var x = 1.1
print(x)
x = 2.2
print(x)
x = 3.3
print(x)
x = 4.4
print(x)

/*:
Create a Boolean variable and set it to `true`. Print the variable, then assign it a value of `false`, and print it again.
*/

var y = true
print(y)
y = false
print(y)

/*:
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.
*/
var x1 = 0
var x2 = 0.0
x1 = Int(x2)

/*:
Create a variable integer with a value of 1,000,000,000. Format it using commas, so it's easier to read.
*/
var z = 1000000000

import Foundation
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
print(numberFormatter.string(from: NSNumber(value:z))!)

/*:
## 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 = 75

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

/*:
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.
*/
print("You can not change a value of a constant, switch to var to make it mutable")

/*:
Declare a variable `age` and set it to your own age. Print `age` to the console.
*/
var age = 26
print(age)

/*:
Now pretend you just had a birthday, and update the `age` variable accordingly. Print `age` to the console.
*/

age = 27
print(age)
/*:

*/


/*:
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).
*/
var x = 1.1
print(x)
x = 2.2
print(x)
x = 3.3
print(x)
x = 4.4
print(x)

/*:
Create a Boolean variable and set it to `true`. Print the variable, then assign it a value of `false`, and print it again.
*/
var y = true
print(y)
y = false
print(y)

/*:
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.
*/
var x1 = 0
var x2 = 0.0
x1 = Int(x2)

/*:
Create a variable integer with a value of 1,000,000,000. Format it using commas, so it's easier to read.
*/
var z = 1000000000
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you could even do something like this:
var z = 1_000_000_000
(The '_' characters get ignored by Swift but maybe make it easier to read what number it is we've written.


import Foundation
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
print(numberFormatter.string(from: NSNumber(value:z))!)

Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

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

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

//friends = 50

/*:
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("You can not change a value of a constant, switch to var to make it mutable")

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

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
percentCompleted = 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,12 @@

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,18 @@

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 = 19
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("You can change the value of a variable")

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

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,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 numLikes = 100
var numComments = 10
let year = 2020
let month = 4
let day = 12



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@
*/



let name = "Omnia"
print("A user's profile name should not change")
var age = 26
print("The user age will automatically increment by years")
var steps = 1286
print("The steps count changes along the day")
var goal = 10000
print("The user might want to change his goal according to his progress")
var HRate = 94
print("The user avg heart rate might change")


/*:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@

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 = 0.1
var secondDecimal = 0.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.
*/

var trueOrFalse = true
//firstDecimal = trueOrFalse
print("we 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 s = "omnia"
//firstDecimal = s
print("we 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 n = 100
//firstDecimal = n
print("we cannot assign value of type 'Int' to type '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 @@ -6,10 +6,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`.
*/

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.
*/
let goal = 10000
var steps = 1286
import Foundation

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
print(numberFormatter.string(from: NSNumber(value:goal))!)
print(numberFormatter.string(from: NSNumber(value:steps))!)



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

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 = "omnia"
print(name)

/*:
Declare a variable called `distanceTraveled` and set it to 0. Do not give it an explicit type.
*/

var distanceTraveled : Double
distanceTraveled = 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
print(distanceTraveled)

//: [Previous](@previous) | page 9 of 10 | [Next: App Exercise - Percent Completed](@next)
Loading