SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36
  1. #26
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    By using jslint repeatedly and researching to understand the errors, I've come to the following:

    HTML Code:
    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.

  2. #27
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The code still isn't working. Do you think defining 'math' is the problem?

  3. #28
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    'math' isn't defined, I think you should be using 'Math'.

    And the following two lines should be ended with a semicolon
    Quote Originally Posted by StevenHu View Post

    HTML Code:
    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);
    }

  4. #29
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks! I did make those two changes, but the code still won't work.

  5. #30
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's your whole code like right now, including the form?

  6. #31
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    <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>
    [/HTML]

  7. #32
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, y'know the moment when it hits you suddenly and you feel silly? I just got that and you might too :P

    Here's a fixed heatpit():

    HTML Code:
    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

    Try fixing formFuel in the same way!

  8. #33
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Wow, at last it works! A hearty thank you!

  9. #34
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Jay.P View Post
    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
    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:

    Code javsacript:
    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.

    Code javascript:
    var stops = parseInt(form.elements.fieldname.value, 10) || 0;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #35
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,316
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Wow, thanks for this valuable information!

  11. #36
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by techforums View Post
    Straight forward division using calculator will give you ...
    And when performing division in binary

    4278 / 60 is equivalent to
    1000010110110 / 111100

    Code:
                 01000111 <-- minutes
           ______________
    111100 )1000010110110
           -111100|||||||
            ------v||||||
            1000010||||||
            -111100||||||
             ------v|||||
                1101|||||
             -111100|||||
              ------v||||
                11011||||
              -111100||||
               ------v|||
                110110|||
               -111100|||
                ------v||
                1101101||
                -111100||
                 ------v|
                 1100011|
                 -111100|
                  ------v
                  1001110
                  -111100
                   ------
                    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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •