Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Set a Null Input to Zero

1. 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?

• 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

• 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.

• Originally Posted by Amphiluke
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?

• 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;```

• Users who have thanked Old Pedant for this post:

ccarrin2 (01-03-2012)

• Originally Posted by Old Pedant
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
•