0
votes

I'm puzzled as to why the result of the Javascript computation does not appear in the input field of id "Between." I ran the JS through jshint.com and it found no errors. This will be used in an iPhone environment on an html page. I'm very new to JS. I picked up some simple math code elsewhere and massaged it into the shape you see here. Can you JS coders show me where my script has gone wrong?



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


1
Can you post the HTML of the form? - DavidJCobb

1 Answers

-1
votes

For one, you've got commas as the end of each statement. they should be semicolons:

var e = form.elements.LENGTH.value;
                                 ^---semicolon
var f = form.elements.STOPS.value;
etc...

Then:

periods = f + '1'

f is retrieved from a form field, and Javascript treats those values as strings, so what you're doing is taking a string, and concatenating with with another string. If the stops value is 123, then you get 1231, not 124.

You then take that string value of 1231 and use it as a divisor. Most likely javascript is casting it to '0', so you're producing a divide-by-zero error.

So... check your browser's javascript error console. In Firefox, just hit shift-ctrl-J to pop it up. And watch for errors as the function executes.