Can anyone tell me where my calculations are going wrong?
The last line gives an utterly ridiculous number (two 4-digit negative numbers for minutes and seconds) rather than the minutes and seconds it should. My commented example calculations are shown to the right of the lines.
Scenario:
A race car driver enters in the length of the race and how long he can drive before he runs out of gas (runtime). He also enters how much time he expects to spend in the pit area for each pit stop.
Purpose:
This calculation tells him how many equally spaced pit stops he’ll need to make throughout the race, and the amount of time driving between pit stops (interval quantity)…
The code check passes in JSHint.com
function heatpit(form) {
var rL = form.elements.raceLength.value, // the length of the race, e.g. 100 min.
rC = form.elements.runtimeComfort.value, // how long you can drive before running out of gas and need pit time to refuel, 10 min.
iL;
if (rL < rC ) {alert("The run time is longer than the race length, so you you won't need any pit times during the race.");}
var pT = form.elements.pitTime.value, // pit time to refuel, 15 secs.
rLsecs = rL * "60", // convert race length into seconds, 6000
rcSecs = rC * "60", // convert runtime into seconds, 600
iQ = (rLsecs / rcSecs) + 1; /* interval quantity, how many times you can run before and after pit stops (1 more than number of pT), 6000 / 585 + 1 = 11 */
if (iQ < 1 ) {iQ = "0";}
iQ = parseInt(iQ, 10); // convert to an integer, 11
var combined = (rcSecs * iQ) + [pT*(iQ-"1")], // add all drive and pit times, 6000 + 15*10 = 6000 + 150=6150 secs
leftOver = combined - rLsecs, // subtract total from race length, 6150 - 6000 = 150 secs
percentage = (leftOver / iQ), // Get the fraction of the leftover time, 150/11=13.64
RT = (rcSecs - percentage); // reduce runtime interval by the fraction of the left over time, 586.36 secs = reduced RT
var RT60 = RT / "60"; // 9.77
var minutes = Math.floor(RT60); // get the minute figure as a digit for the time readout, 9
var seconds = RT60 % "60"; // use modulus operator to get the remainder and convert into seconds figure for the time readout, 46
seconds = parseInt(seconds, 10);
if (seconds === 0) {seconds = "00";}
var iL2 = minutes + ":" + seconds; // concatenate everything into minutes and seconds
form.elements.iQ.value = iQ;
form.elements.iL.value = iL2;
}