These exercises reinforce Swift concepts in the context of a fitness tracking app.
In many cases you want to provide input to a function. For example, the progress function you wrote in the Functioning App exercise might be located in an area of your project that doesn't have access to the value of steps and goal. In that case, whenever you called the function, you would need to provide it with the number of steps that have been taken and the goal for the day so it can print the correct progress statement.
Rewrite the function progressUpdate, only this time give it two parameters of type Int called steps and goal, respectively. Like before, it should print "You're off to a good start." if steps is less than 10% of goal, "You're almost halfway there!" if steps is less than half of goal, "You're over halfway there!" if steps is less than 90% of goal, "You're almost there!" if steps is less than goal, and "You beat your goal!" otherwise. Call the function and observe the printout.
Call the function a number of times, passing in different values of steps and goal. Observe the printouts and make sure what is printed to the console is what you would expect for the parameters passsed in
Answer:
Here is the swift code as per your requirement
Raw code:
// function with parameter step and goal of data types int
func progressUpdate(step:Int,goal:Int){
//variable to calculate and 10% percent of goal
let tenPercentOfGoal:Double=(11/100)*Double(goal)
//variable to calculate and 50% or half of goal percent of goal
let halfOfGoal:Double=(50/100)*Double(goal)
//variable to calculate and 90% percent of goal
let ninetyPercentOfGoal:Double=(90/100)*Double(goal)
// checking if steps are less than 10 % of goal
if Double(step)<tenPercentOfGoal{
print("You're off to a good start.")
}
//checking if steps are less tha 50%of goal
else if Double(step)<halfOfGoal{
print("You're almost halfway there!")
}
//checking if steps are less than 90 of goal
else if Double(step)<ninetyPercentOfGoal{
print( "You're over halfway there!")
}
//if they are abouve 90 print this
else{
print("You beat your goal!")
}
}
// calling function to check below 10
progressUpdate(step:9,goal:100)
// calling function to check below 90
progressUpdate(step:55,goal:100)
// calling function to check below 50
progressUpdate(step:43,goal:100)
// calling function to check abouve 90
progressUpdate(step:109,goal:100)
Editor:
output:
Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.
"Please refer to the screenshot of the code to understand the indentation of the code".
Thank you! Do upvote.
Get Answers For Free
Most questions answered within 1 hours.