How do i put switch inside if condition?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

How do i put switch inside if condition??

I want to put switch inside if condition. And the switch has a variable that contain a value of Number(prompt()). If i try to run the code, nothing appears. See what i mean: var s1 = Number(prompt("Which Number?")); if(s1 == Number) { switch(s1) { case "1": document.write(" I am number 1"); break; case "2": document.write(" I am number 2"); break; case "3": document.write(" I am number 1"); break; default: document.write("No number found"); } }

20th Oct 2020, 10:37 AM
R£∆L ∆LILI
R£∆L ∆LILI - avatar
2 Answers
+ 2
if(typeof(s1)=="number")
20th Oct 2020, 10:44 AM
Abhay
Abhay - avatar
+ 1
Currently you are checking if a value is equal to function. Check if s1 is not NaN. if (!isNaN(s1)) { }
20th Oct 2020, 10:58 AM
Ankit