+ 1

Why does this code print n ??

j script https://code.sololearn.com/W3nv9xu950Va/?ref=app

24th Nov 2017, 4:51 PM
Srr
Srr - avatar
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") }
24th Nov 2017, 5:05 PM
Dev
Dev - avatar
+ 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.
24th Nov 2017, 5:12 PM
Paul
Paul - avatar
0
thanks...
24th Nov 2017, 5:19 PM
Srr
Srr - avatar