Jump to content

Variable Woes (i Think)


Mikeyham

Recommended Posts

Started with my JavaScript bible about a week ago, and I'm up to if & else schtuff. I made this just for practice, but I can't get it to work! =(

var inputAge = document.getElementById("ageInput").value;var userAge = parseFloat(inputAge);function dispAgeReq(){   if (userAge < 18)   {   document.getElementById("divList").innerHTML = "Sorry, you do not meet the age specifications for this site.";   }   else if (userAge = NaN)   {   document.getElementById("divList").innerHTML = "Please insert a number value.";   }   else   {   document.getElementById("divList").innerHTML = "Thank you, please proceed onto the site.";   }}

where ageInput is the text box that the user inserts their age into, and divList displays the results. It displays the else statements no matter what I put into the text field, which leads me to believe that it's something with the userAge variable (or inputAge) that's causing the problem, because else is the only one that doesn't require arguments.

Link to comment
Share on other sites

1. inputAge and userAge seem to be defined in the global space of the script -- that is, not in a function. This means their values are initialized when the script is downloaded. At this point, the "ageInput" element probably doesn't exist in the DOM yet, so it has no value. In any case, you do not assign its value to inputAge in the dispAgeReq() function, which is where you need to assign it if this whole thing is going to work. Put those first two statements in the function.2. (userAge = NaN) is an assignment, not a comparison. For a comparison, you want the == operator, not the = operator.3. But it's not a normal comparison. To determine if something is NaN, use the isNaN() function. I don't know why this is, but it is.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...