Search code examples
iosxcodemacosswift3swift-playground

How to solve this lesson 2.2 lab in Apple's App Development With Swift iBook?


I am a noob trying to learn swift with Apple's "App Development With Swift" iBook and can't seem to complete the final lab in lesson 2.2 Functions. Can anyone guide me with the correct way to complete the lab? Here is what the lab is asking: Now write a function called pacing that takes four Doubles arguments called currentDistance, totalDistance, currentTime, and goalTime. The function should also return a String, which will be the message to show the user. The function should call calculatePace, passing in the appropriate values, and capture the return value. The function should then compare the returned value to goalTime and if the user is on pace return "Keep it up!", and return "You've got to push it a bit harder!" otherwise. Call the function and print the return value.

Here is my calculatePace function from earlier in a different part of the lab:

func calculatePace(currentDistance: Double, totalDistance: Double, currentTime: Double) -> Double {
    let currentSpeed = currentDistance / currentTime
    return ((totalDistance / currentSpeed) / 60)
}
print("\(calculatePace(currentDistance: 1, totalDistance: 10, currentTime: 6)) hours till you finish the run!")

Here is the function I am trying to make to solve the lab with but am having trouble with it:

func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) -> String {
    //I don't know what to put in return
    return ("test return")
    calculatePace(currentDistance: 1, totalDistance: 10, currentTime: 6, goalTime: 60)
}
pacing(currentDistance: 1, totalDistance: 10, currentTime: 6, goalTime: 60)

I don't understand how I am supposed to capture the return value and the last few sentences of the lab confuse me. "And if the user is on pace return "keep it up!", and return "You've got to push it just a bit harder!" otherwise. Call the function and print the return value." Aren't those two different prints so how do I return both and what does the otherwise part mean?


Solution

  • I think this is what you want , Please check..

     func calculatePace(currentDistance: Double, totalDistance: Double, currentTime: Double) -> Double {
            let currentSpeed = currentDistance / currentTime
            return (totalDistance / currentSpeed)
     }
     print("\(calculatePace(currentDistance: 1, totalDistance: 10, currentTime: 6)) hours till you finish the run!")
    
     func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) -> String {
            let yourFinishTime = calculatePace(currentDistance: currentDistance, totalDistance: currentDistance, currentTime: currentTime)
            var message = ""
    
            if yourFinishTime > goalTime {
                message = "You've got to push it a bit harder!"
            }else {
                message = "Keep it up"
            }
    
            return message
    
    }
    pacing(currentDistance: 1, totalDistance: 10, currentTime: 6, goalTime: 60)
    

    Explanation: First of all I used currentDistance and currentTime to find the current speed,then I find the time or you can say etimated time that I would take to complete the total distance if I keep going on the current speed, I returned this time from calculatePace function , then I compared this time with goal time , if I am taking more time to complete the total distance then I would have to push harder otherwise just keep up. Hope it helps.