Jump to content

Variable Woes (i Think)


Mikeyham
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...