+ 1
Why does this code print n ??
3 Answers
+ 11
Switch-case doesn't works like that, try this instead:
var age=17;
switch(age){
case(17):
document.write ("not adult")
break ;
case(18):
document.write("young adult")
break ;
case (30 ) :
document.write ("adult")
break ;
case(44):
document.write ("elder")
break ;
case(55):
document.write ("old person")
break ;
default :
document.write ("n")
}
+ 3
Or try it like this::
I only changed the argument on the switch statement. Expressions evaluate into true or false.
var age=19;
switch(true){
case(age==17):
document.write ("not adult")
break ;
case(age>18):
document.write("young adult")
break ;
case (age>30 ) :
document.write ("adult")
break ;
case(age >44):
document.write ("elder")
break ;
case(age >55):
document.write ("old person")
break ;
default :
document.write ("n")
}
Found it on stack overflow.
0
thanks...