0
Why the answer is 8 please ??? I don't understand :'(
What is the output of this code? var x = 3; switch (x) { case 1: document.write(x); break; case 2: document.write(x+2); break; default: document.write(x+5); } ANSWER: 8
5 odpowiedzi
+ 4
Because it will check if x is 1 and then if it is, it will write the value x.
Right now it isnt 1 nor 2. So it goes to the default case
+ 4
Because you have defined a switch and the first case is 1.
It will check if it is 1
+ 3
switch value of variable <x>
Is it 1?
If it is then print <x>
Break out
Is it 2?
If it is then print <x> plus 2
Break out
Otherwise
print <x> plus 5
Knowing <x> value is 3, the first two evaluations was proven wrong (false). And since there are no other conditions to check against value of <x>, we end up in the "Otherwise" block.
+ 2
Thank you for your answer but I still don't understand i'm sorry. And why 1 now ? :'(
+ 2
Hummm... I see... Ok ! Great. Thank you :-)