SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,338
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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;
                }

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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;
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,338
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I made your change, but it still gives me a 4-digit number for the minutes.

    Thanks!

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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;
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,338
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •