PDA

View Full Version : add/concatenate?? so annoying....



HappyTomato
Dec 5th, 2002, 03:06 AM
we've got this function that we want to calculate some values with.... however for some reason it's reading the variables(in orange) "hour", "sHour", "min" and "sMin" as strings and concatenated them instead..(however inside the else statements, the two statements did the calculations as if they were ints.... ).... we don't know how to specifically assign a variable to be an int, so it'll add....

function changeFTime() {

var sHour = document.projectForm.inputSHour.value;
var sMin = document.projectForm.inputSMin.value;
var hour = document.projectForm.inputFHours.value;
var min = document.projectForm.inputFMin.value;
var finalEHour = finalEMin = 0;

finalEHour = hour + sHour;

finalEMin = min + sMin;

if(finalEMin<60){
document.projectForm.inputEHour.value = finalEHour;
document.projectForm.inputEMin.value = finalEMin;
}
else{
finalEMin = finalEMin - 60;
finalEHour = finalEHour - 1;
document.projectForm.inputEHour.value = finalEHour;
document.projectForm.inputEMin.value = finalEMin;
}
}


please help..
:confused:


Bo & Vic

glenngv
Dec 5th, 2002, 03:19 AM
finalEHour = parseInt(hour,10) + parseInt(sHour,10);

finalEMin = parseInt(min) + parseInt(sMin);

if the string cannot be converted to integer, NaN is returned.
Since the values are user inputs, you should first check if they entered valid numbers.

if (isNaN(sHour)){
alert("Please enter valid hour.");
return;
}
...