Why does this code not work!


#1

var e
var answer = prompt("What is your favourite single digit number","Write a number from 0 to 9");
var e = (isNaN(answer))
if (e) {
   switch(answer) {
 case '0':
     console.log("single digit, but not sure if number")
     break;
     default ' ':
        console.log("I love this number aswell!") 
}
}
else
{
    console.log("SIKE, THATS THE WRONG NUMBER! (or not one)")
    
}

why dont this work?


#2

remove ' ' from

default ' ' //here


#3

default can't have a condition:

default:
   // do something

since default is everything else, you can't specify a case.

also, there is a problem here:

var e = (isNaN(answer))

if answer is a number, the condition evaluates to false (number is not a number, well, it is a number, so it is false), otherwise true. You should reverse it (you can use a !.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.