ccarrin2
01-02-2012, 04:11 PM
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?
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?