Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-02-2012, 04:11 PM   PM User | #1
ccarrin2
New Coder

 
Join Date: Jan 2012
Posts: 41
Thanks: 6
Thanked 0 Times in 0 Posts
ccarrin2 is an unknown quantity at this point
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?
ccarrin2 is offline   Reply With Quote
Old 01-02-2012, 04:36 PM   PM User | #2
ccarrin2
New Coder

 
Join Date: Jan 2012
Posts: 41
Thanks: 6
Thanked 0 Times in 0 Posts
ccarrin2 is an unknown quantity at this point
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
ccarrin2 is offline   Reply With Quote
Old 01-02-2012, 05:28 PM   PM User | #3
Amphiluke
Regular Coder

 
Amphiluke's Avatar
 
Join Date: Jul 2009
Posts: 312
Thanks: 3
Thanked 89 Times in 89 Posts
Amphiluke is on a distinguished road
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
Amphiluke is offline   Reply With Quote
Old 01-02-2012, 08:57 PM   PM User | #4
ccarrin2
New Coder

 
Join Date: Jan 2012
Posts: 41
Thanks: 6
Thanked 0 Times in 0 Posts
ccarrin2 is an unknown quantity at this point
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?
ccarrin2 is offline   Reply With Quote
Old 01-02-2012, 10:06 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
ccarrin2 (01-03-2012)
Old 01-02-2012, 11:55 PM   PM User | #6
ccarrin2
New Coder

 
Join Date: Jan 2012
Posts: 41
Thanks: 6
Thanked 0 Times in 0 Posts
ccarrin2 is an unknown quantity at this point
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!
ccarrin2 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:53 AM.


Advertisement
Log in to turn off these ads.