Time is a notoriously hard thing to compute with. Yes, before you do arithmetic you need to get everything into seconds. But if you ask for minutes and seconds as distinct inputs you should be able to do that arithemetic.
- ask for hours, mins, secs
- convert to secs (hours * 60 * 60 + mins * 60 + secs)
- calculate pace in secs (e.g. totalSecs / numMiles)
- convert back to separate hours, mins, secs. <-- this is where the arithmetic has to be thoughtful.
One thing to take advantage of is a function called
parseInt(...) for which there is no block, but you can type it.
parseInt will return the whole number portion of a decimal value. SO
parseInt(32.1) evaluates to 32.
parseInt(32.9999) also evaluates to 32.
This is convenient if you’re trying to do something like arithmetic to convert seconds back into minutes + seconds. For example. If you have 347 seconds, you could divide 347/60 to get the number of minutes. 347/60 evaluates to 5.78333… 5 mins + some number of seconds. but
parseInt(347/60) evaluates to 5. 5*60 is 300 seconds. So then 347-300 is 47. So you can work out that 347 seconds is 5 mins 47 seconds.
var totalSecs = //...some large number of seconds
var mins = parseInt(totalSecs/60);
var secs = totalSecs - (mins*60);
console.log("pace = "+mins+"m "+secs+"s");
I think it’s within bounds to tell the student about the
parseInt function and how it can be used to help this arithmetic.