0

My problem is, when you type an age, it always says "Invalid Age" w/c falls in the default condition. I dont know how to figure out this. Its my codes. Help me to fix this. Thanks!

<html>
<body>
<script type="text/javascript">

var $age = Number(prompt("Enter Your Age: "));

switch ($age)
{

case ($age>='1' && $age<='8') :
document.write('Paramihan ng Toys!');
break;

case ($age>='9' && $age<='18') :
document.write('Pataasan ng Grades!');
break;

case ($age>='19' && $age<='25') :
document.write('Padamihan ng Syota!');
break;

case ($age>='26' && $age<='35') :
document.write('Pagandahan ng Asawa!');
break;

case ($age>='36' && $age<='45') :
document.write('Palakiha ng Income!');
break;

case ($age>='46' && $age<='55') :
document.write('Padamihan, Pagandahan at Pabataan ng kabit!');
break;

case ($age>='56' && $age<='70') :
document.write('Padamihan ng sakit!');
break;

case ($age>='71') :
document.write('Pabonggahan ng Libing!');
break

default :
document.write("Invalid Age!");
break;
}

</script>
</body>
</html>

I hope you can help me.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by choboja621
0

Your mistakes,
1) with case you can not write condition
2) ignore using single quotes when comparing numbers
3) $ sign is not applicable to pure javascript. use variables without $ sign

<html>
<body>
<script type="text/javascript">

var age = Number(prompt("Enter Your Age: "));



if(age>=1 && age<=8) {
document.write('Paramihan ng Toys!');
}

else if(age>9 && age<18) {
document.write('Pataasan ng Grades!');
}

else if(age>19 && age<25) {
document.write('Padamihan ng Syota!');
}

else if(age>26 && age<35) {
document.write('Pagandahan ng Asawa!');
}

else if(age>36 && age<45) {
document.write('Palakiha ng Income!');
}

else if(age>46 && age<55) {
document.write('Padamihan, Pagandahan at Pabataan ng kabit!');
}

else if(age>56 && age<70) {
document.write('Padamihan ng sakit!');
}
else if(age>71) {
document.write('Pabonggahan ng Libing!');
}
else {
document.write("Invalid Age!");
}


</script>
</body>
</html>
0

Your mistakes,
1) with case you can not write condition
2) ignore using single quotes when comparing numbers
3) $ sign is not applicable to pure javascript. use variables without $ sign

<html>
<body>
<script type="text/javascript">

var age = Number(prompt("Enter Your Age: "));



if(age>=1 && age<=8) {
document.write('Paramihan ng Toys!');
}

else if(age>9 && age<18) {
document.write('Pataasan ng Grades!');
}

else if(age>19 && age<25) {
document.write('Padamihan ng Syota!');
}

else if(age>26 && age<35) {
document.write('Pagandahan ng Asawa!');
}

else if(age>36 && age<45) {
document.write('Palakiha ng Income!');
}

else if(age>46 && age<55) {
document.write('Padamihan, Pagandahan at Pabataan ng kabit!');
}

else if(age>56 && age<70) {
document.write('Padamihan ng sakit!');
}
else if(age>71) {
document.write('Pabonggahan ng Libing!');
}
else {
document.write("Invalid Age!");
}


</script>
</body>
</html>

Thanks! :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.