# Why doesn't my math add up?

Printable View

• Jun 20, 2011, 13:38
StevenHu
Why doesn't my math add up?
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

Code:

```  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;             }```
• Jun 20, 2011, 14:05
felgall
Code:

```var RT60 = RT / "60";                 var minutes = Math.floor(RT60);                 var seconds = RT60 % "60";```
If dividing by 60 gives the minutes then taking the mod of the minutes doesn't give the seconds.

Assuming that the minutes are being correctly calculated by the first two of those statements, you'd need the following to get the seconds.

Code:

`var seconds = RT % 60;`
• Jun 20, 2011, 14:16
StevenHu
I made your change, but it still gives me a 4-digit number for the minutes.

Thanks!
• Jun 20, 2011, 22:52
felgall
With the following statement

Code:

`RT = (rcSecs - percentage);`
is percentage the amount you want to subtract or is it a percentage as its name suggests. If it is a percentage to be sibtracted then it should be

Code:

`RE = rcSecs * (100-percentage)/100;`
• Jun 21, 2011, 09:28
StevenHu
Percentage is poorly named. It is really just a fraction. I take the excess time past the race length, and divide it by the intervals to get a fraction of the interval, then subtract that fraction from the original interval to get the run times and pit times to equal the race length.