Why the answer is 8 please ??? I don't understand :'( | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

5th Oct 2020, 7:17 AM
Nadja
5 Answers
+ 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
5th Oct 2020, 7:19 AM
Steve Sajeev
Steve Sajeev - avatar
+ 4
Because you have defined a switch and the first case is 1. It will check if it is 1
5th Oct 2020, 7:28 AM
Steve Sajeev
Steve Sajeev - avatar
+ 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.
5th Oct 2020, 7:48 AM
Ipang
+ 2
Thank you for your answer but I still don't understand i'm sorry. And why 1 now ? :'(
5th Oct 2020, 7:26 AM
Nadja
+ 2
Hummm... I see... Ok ! Great. Thank you :-)
5th Oct 2020, 8:10 AM
Nadja