I was hoping not to post the actual code, in order to force you to work it out yourself but, since @Torust has already done that, let me give you my version, on the premise that slightly different explanations can sometimes clarify better than one.
I will start by asking why are you nesting a "do nothing" calculatedPace
function inside the pacing
function? What you should be doing is calling the calculatePace
function you already wrote above.
But then you should also correct the logic in that :
func calculatePace(currentDistance: Double, totalDistance: Double, currentTime: Double) -> Double
{
return currentTime * (totalDistance / currentDistance)
}
(using parentheses around the ratio division helps to make the operator precedence more obvious)
Then you need to call that function from the pacing function :
func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) -> String
{
let currentArrivalTime = calculatePace(currentDistance: currentDistance, totalDistance: totalDistance, currentTime: currentTime)
if currentArrivalTime > goalTime
{
return "You've got to push it just a bit harder!"
}
else
{
return "Keep it up!"
}
}
Notice the use of a variable to hold the result of the function, so that you can then compare it to the goalTime
parameter.
Finally, you need to call the pacing
function and, once again, you should assign the result to a variable, so that you can then pass to something else, without having to call the function again
let message = pacing(currentDistance: 2.4, totalDistance: 4.0, currentTime: 60, goalTime: 120)
print(message)
Just out of interest, there is another way of organising the two functions, just to show the principle of separating functions is more flexible than just the example given in the lab
func calculatePace(distance: Double, time: Double) -> Double
{
return distance / (time / 60) // assumes time is minutes and speed returned is mph
}
func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) -> String
{
let currentSpeed = calculatePace(distance: currentDistance, time: currentTime)
let goalSpeed = calculatePace(distance: totalDistance, time: goalTime)
return currentSpeed < goalSpeed ? "You've got to push it just a bit harder!" : "Keep it up!"
}