How do I convert a digit (430) into minutes/seconds (7:30)?

It points to the beginning of this line, and says a semicolon is missing. Strange!

var resultPitStops = document.getElementById(‘resultPitStops’);

That’s where the problem starts. Look at the end of the previous line.

It also ends in a semi-colon. I don’t see the problem:

var runtime = document.getElementById(‘runtime’);
runtime.onclick = function () {
var form = document.getElementById(‘form1’);
formFuel(form);
}

Are you sure it’s only the javascript that you’re submitting there?

I get a “SyntaxError: missing variable name” error at a very different place when submitting your scripting code.

Oh, I pasted in the whole page!

By using jslint repeatedly and researching to understand the errors, I’ve come to the following:

var document,
    runtime = document.getElementById('runtime'),
    resultPitStops = document.getElementById('resultPitStops');


function formFuel(form) {
    var a = form.elements.tank.value,
        b = form.elements.driveMinutes.value,
        c = form.elements.driveSeconds.value,
        d = form.elements.consumed.value,
        used = a - d, // get the amount of fuel used  -->
        secs = b * 60, // convert minutes into seconds  -->
        secstotal = secs + c, // add minutes and seconds to get total seconds  -->
        secsPerCC = secstotal / used, // divide the number of seconds into the used fuel amount for secs per cc  -->
        seconds = secsPerCC % 60, // use modulus operator to get the remainder and convert into seconds figure  -->
        minutes = math.floor(secsPerCC / 60),  // get the minute figure as a digit  -->
        runtime2 = minutes + ":" + seconds; // concatenate everything into minutes and seconds  -->

    form.elements.ans.value = runtime2;
}

function heatpit(form) {
    var e = form.elements.LENGTH.value, // put LENGTH field value into variable e  -->
        f = form.elements.STOPS.value, // put STOPS field value into variable f  -->
        secs2 = e * 60, // convert the minutes into seconds  -->
        periods = f + '1', // add 1 to the pit stop number get the total  number of divisions throughout the period  -->
        result2 = secs2 / periods, // divide the full time by the number of periods  -->
        seconds = result2 % 60, // use modulus operator to get the remainder and convert into seconds figure  -->
        minutes = math.floor(result2 / 60), // get the minute figure as a digit  -->
        time = minutes + ":" + seconds; // concatenate everything into minutes and seconds  -->

    form.elements.between.value = time; // display in the "between" field s/b "time" value  -->
}

runtime.onclick = function () { // https://developer.mozilla.org/en/DOM/element.onclick
    var form = document.getElementById('form1');
    formFuel(form);
}

resultPitStops.onclick = function () {
    var form = document.getElementById('form2');
    heatpit(form);
}

I still don’t understand why a semi-colon is a problem inside the last two functions. When I remove them, then more errors appear. I also don’t know how to ‘define’ math.

This has been a good learning experience.

The code still isn’t working. Do you think defining ‘math’ is the problem?

‘math’ isn’t defined, I think you should be using ‘Math’.

And the following two lines should be ended with a semicolon

Thanks! I did make those two changes, but the code still won’t work.

What’s your whole code like right now, including the form?

<script language=“JavaScript”>

function heatpit(form) {
var e = form.elements.LENGTH.value, // put LENGTH (number of minutes) field value into variable e
f = form.elements.STOPS.value, // put STOPS field value into variable f
secs2 = e * 60, // convert the minutes into seconds
periods = f + ‘1’, // add 1 to the pit stop number get the total number of divisions throughout the LENGTH
result2 = secs2 / periods, // divide the LENGTH by the number of periods
seconds = result2 % 60, // use modulus operator to get the remainder and convert it into seconds figure
minutes = Math.floor(result2 / 60), // get the minute figure as a digit
time = minutes + “:” + seconds; // concatenate everything into minutes and seconds

form.elements.between.value = time; // display "time" value in the "between" field

}

var resultPitStops = document.getElementById(‘resultPitStops’);
resultPitStops.onclick = function () {
var form = document.getElementById(‘form2’);
heatpit(form);
};

</script>

</body>
</html>

Ahh, y’know the moment when it hits you suddenly and you feel silly? I just got that and you might too :stuck_out_tongue:

Here’s a fixed heatpit():


function heatpit(form) {
    var e = form.elements.LENGTH.value, // put LENGTH (number of minutes) field value into variable e
        f = parseInt(form.elements.STOPS.value), // put STOPS field value into variable f
        secs2 = e * 60, // convert the minutes into seconds
        periods = f + 1, // add 1 to the pit stop number get the total  number of divisions throughout the LENGTH
        result2 = secs2 / periods, // divide the LENGTH by the number of periods
        seconds = result2 % 60, // use modulus operator to get the remainder and convert it into seconds figure
        minutes = Math.floor(result2 / 60), // get the minute figure as a digit
        time = minutes + ":" + seconds; // concatenate everything into minutes and seconds

    form.elements.between.value = time; // display "time" value in the "between" field
}

The changes being:
f = parseInt(form.elements.STOPS.value)
and
periods = f + 1

see, the form field is a string, making f a string that just happened to contain an integer.
if you typed ‘1’ into the form, it’d calculate:
periods = ‘1’ + ‘1’;
to get the result ‘11’!

parseInt() converts to to an integer, and removing the quotes around 1 stops it being converted back to append the ‘1’ character :smiley:

Try fixing formFuel in the same way!

Wow, at last it works! A hearty thank you!

If this helps, it can be a good practice to always use Number(form.elements.fieldname.value) whenever you expect the value to be used as a number.

That way, if it’s not a number you can provide a default for it instead:


var stops = Number(form.elements.fieldname.value) || 0;

And of course when it absolutely must be a whole number, use parseInt but don’t forget to include the radix indicator too, otherwise entries of “08” or “09” won’t end up as you expect them to be.


var stops = parseInt(form.elements.fieldname.value, 10) || 0;

Wow, thanks for this valuable information!

And when performing division in binary

4278 / 60 is equivalent to
1000010110110 / 111100


             01000111 <-- minutes
       ______________
111100 )1000010110110
       -111100|||||||
        [COLOR="DarkRed"]------[/COLOR]v||||||
        1000010||||||
        -111100||||||
         [COLOR="DarkGreen"]------[/COLOR]v|||||
            1101|||||
         -111100|||||
          [COLOR="DarkRed"]------[/COLOR]v||||
            11011||||
          -111100||||
           [COLOR="DarkRed"]------[/COLOR]v|||
            110110|||
           -111100|||
            [COLOR="DarkRed"]------[/COLOR]v||
            1101101||
            -111100||
             [COLOR="DarkGreen"]------[/COLOR]v|
             1100011|
             -111100|
              [COLOR="DarkGreen"]------[/COLOR]v
              1001110
              -111100
               [COLOR="DarkGreen"]------[/COLOR]
                10010 <-- seconds


The quotient is the number of minutes, and the remainder is the number of seconds.

01000111 makes for 79 minutes, and 10010 is 26 seconds.
But this has a similar lack of relevance as that with post #36