var age = prompt('How old are you?');
if ('age' <= 5){
alert("You are a baby!");
}
else if ('age' >= 5 && 'age' < 18){
alert("You are a child!");
}
else if ('age' >= 18 && 'age' < 70){
alert("You are an adult!");
}
else
alert("You are a senior!");
So I am trying to make it change depending on user input for age. However, no matter the age I enter "You are a senior!" is the only alert that goes off.
2
Answers
You need to remove those inverted commas ‘ ‘ or quotes around
age
within If condition. By putting'age'
, you are using it as a string, but it is a variableage
.the problem is that when you make the comparison
if ('age' <= 5)
because of the quotes surrounding age is saying "if the string age is less than the number five" and that won’t ever be true. Age is a variable that contains a number so as with all variables it shouldn’t be surrounded by quotation marks.