Skip to content

1.Getting started EgyptSpring2020 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
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,13 +10,14 @@ 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("lullaby")

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


print("The sky is amazing")
print("just like your eye")

/*:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
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.
*/

var friends = 500


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


/*:
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("let makes variable unchangable, so we need to make it var\(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

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

Your fitness tracking app needs to know goal number of steps per day. Create a constant `goalSteps` and set it to 10000.
*/
let goalsteps = 3000 // now this is a constant not a variable



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

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 = 9
print(schooling)


/*:
Now imagine you just completed an additional year of school, and update the `schooling` variable accordingly. Print `schooling` to the console.
*/
schooling = 10
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("because var is a variable we can update the value several times")
//: [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,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("Good Morning your start setp number is : \(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 ( "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 @@ -12,6 +12,22 @@
*/


var likes = 0
var comments = 0
let year = 2020
let month : String
month = "june"
let day = 1



/* Name: The user's name
- Age: The user's age
- Number of steps taken today: The number of steps that a user has taken today
- 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 */





Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
- Average heart rate: The user's average heart rate over the last 24 hours
*/

let name : String
print ("A person name wont change")
let age = 0
print("A person age wont change")
var steps2 = 0
print("number of steps taken need to be updated along the day")
let goal2 = 0
print("A person goal wont change")
var heartrate = 0
print("heartrate will change according to activity")



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

/*:
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("cant assign boolean to 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 trueOrFalse2 = "true"
//firstDecimal = trueOrFalse2
print("cant 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 trueOrFalse3 = 3
//firstDecimal = trueOrFalse3
print("cant assign int 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 @@ -6,10 +6,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.
*/

let goalnumber = 20_000
var currentstepcount = 80_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,23 @@

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

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

var distanceTraveled : Double

/*:
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 @@ -23,7 +23,12 @@
/*:
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
let double3 : Double
double10 = 10
double3 = 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 @@ -34,5 +39,9 @@
*/
let pi = 3.1415927

let radius = 5.0

let diameter = 2 * radius

let circumference = 2 * pi * radius
//: 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,36 @@

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 = 60
let heartRate2 = 80
let heartRate3 = 100
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
let heartRate2D : Double
let heartRate3D : Double

heartRate1D = 60
heartRate2D = 80
heartRate3D = 100
let addedHRD = heartRate1D + heartRate2D + heartRate3D
let averageHRD = addedHRD / 3
print(averageHRD)
print("yes because they are all double values")

/*:
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
let goal : Double
steps = 3_467
goal = 10_000
let percentOfGoal = (steps/goal)*100
print(percentOfGoal)

//: [Previous](@previous) | page 2 of 8 | [Next: Exercise - Compound Assignment](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
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 value = 10
value = value + 5
print(value)
value *= 5
print(value) // compound operator
/*:
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 @@ -18,6 +22,17 @@
*/


var piggybank = 0
piggybank += 10
print(piggybank)
piggybank += 20
print(piggybank)
piggybank /= 2
print(piggybank)
piggybank *= 3
print(piggybank)
piggybank -= 3
print(piggybank)



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

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
distance = 50
distance *= 3
print(distance)



//: [Previous](@previous) | page 4 of 8 | [Next: Exercise - Order of Operations](@next)
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
Print out what you think 10 + 2 * 5 evaluates to. Then print out the actual expression (i.e. `print(10 + 2 * 5)`)
*/


var myanswer = 20
var trueanswer = 10 + 2 * 5
print(trueanswer)
/*:
In a separate `print` statement, add in the necessary parentheses so that addition takes place before multiplication.
*/


print(((10 + 2) * 5))
/*:
Print out what you think 4 * 9 - 6 / 2 evaluates to. Then print out the actual expression.
*/

print(33)
print(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(4 * (9 - 6) / 2 )// - * /

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

/*:
## App Exercise - Complex Fitness Calculations

>These exercises reinforce Swift concepts in the context of a fitness tracking app.

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.
*/
var heartRate1 : Double
var heartRate2 : Double
var heartRate3 : Double

heartRate1 = 60
heartRate2 = 80
heartRate3 = 100

let avg = (heartRate1+heartRate2+heartRate1) / 3
print(avg)


/*:
One feature you might want to give users is to display their current body temperature. Create a constant `tempInFahrenheit` equal to 98.6.

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/9)
print(tempinCelsius)


//: [Previous](@previous) | page 6 of 8 | [Next: Exercise - Numeric Type Conversion](@next)
Loading