SpydreX SpydreX - 2 years ago 103
iOS Question

How can I keep track if runner is on pace in fitness tracking app?

I am going through the challenges in Apple's "App Development With Swift" iBook and have hit a roadblock in completing the fitness app in Lesson 2.2 - Functions. I can't think of a good formula to track if the user is on pace or not. I am still a noob and this is so far what I have come up with which obviously doesn't accurately keep track of the pace.

func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) {

if (currentDistance < 0.50 * totalDistance && currentTime > 0.40 * goalTime) {
print("You've got to push it just a bit harder!")
}
else {
print("Keep it up!")
}
}
pacing(currentDistance: 1, totalDistance: 10, currentTime: 8, goalTime:60)


The challenge in the book tells you to do the following:
Your fitness tracking app is going to help runners stay on pace to reach their goals. Write a function called pacing that takes four Double parameters called currentDistance, totalDistance, currentTime, and goalTime. Your function should calculate whether or not the user is on pace to hit or beat goalTime. If yes, print "Keep it up!", otherwise print "You've got to push it just a bit harder!"

Answer Source

As we know that Distance = Speed * Time , so here you want to know that what is the current speed and based on that you would print the appropriate message So you can try something like this:

func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) {

        let goalSpeed = totalDistance / goalTime
        let currentSpeed = currentDistance / currentTime

        if (currentSpeed < goalSpeed)     {
                print("You've got to push it just a bit harder!")
        }
        else {
                print("Keep it up!")
        }
}
pacing(currentDistance: 1, totalDistance: 10, currentTime: 8, goalTime:60)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download