Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Set a Null Input to Zero

    I'm guessing this question will be one of the easiest questions ever posted but I'm just not getting this fix. I have a simple Javascript program that calculates a users pace for their run. The inputs are done through an HTML Form.

    The user can input hours, minutes, & seconds of their run. Then the distance number & measurment. Then finally the pace desired. I am having some trouble with null values in this program. If I input a 0 in hours and then fill everything out the program works great. 100%! But, if I leave the field blank and hit calculate I get a NaN. I'm guessing I need some kind of statement like if this value is null set it to zero but I am not having any luck with exactly what kind of syntax I need. So here is the code, again not very complex, any help would be greatly appreciated!


    function calculateTime(){
    var time_hour = parseInt(document.getElementById("time_hour").value);
    var time_min = parseInt(document.getElementById("time_minute").value);
    var time_sec = parseFloat(document.getElementById("time_second").value);
    var dist_num = parseFloat(document.getElementById("dist_num").value);
    var pace_choice = document.getElementById("pace_choice").selectedIndex;
    var dist_choice = document.getElementById("dist_choice").selectedIndex;
    var total_sec = (time_hour * 3600) + (time_min * 60) + (time_sec);

    // Then I'm guessing my if statement would go here somewhere?

  • #2
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Here is the pace calculator! As you will see if you fill in all values everything works fine. But if you leave hour blank NaN

    http://www.gmustudent.com/calc.html

  • #3
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    This example should help:
    Code:
    var t = "";
    parseInt(t); // NaN
    parseInt(+t); // 0
    Note that the unary "+" operator returns its operand transformed into a number.
    I am still learning English

  • #4
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Amphiluke View Post
    This example should help:
    Code:
    var t = "";
    parseInt(t); // NaN
    parseInt(+t); // 0
    Note that the unary "+" operator returns its operand transformed into a number.
    So in order to do what you are saying should I replace t with time_hour in order to set it to 0?

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Not quite.

    Code:
    var time_hour = parseInt(+document.getElementById("time_hour").value);
    But you could also be more explicit about it:
    Code:
    var time_hour = parseInt(document.getElementById("time_hour").value);
    if ( isNaN(time_hour) ) time_hour = 0;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    ccarrin2 (01-03-2012)

  • #6
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Not quite.

    Code:
    var time_hour = parseInt(+document.getElementById("time_hour").value);
    But you could also be more explicit about it:
    Code:
    var time_hour = parseInt(document.getElementById("time_hour").value);
    if ( isNaN(time_hour) ) time_hour = 0;
    WOW Thank you so much!!! This was my first question ever on a forum and it was awesome! Thank you again!


  •  

    Posting Permissions

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