IsNaN within switch?


#1



https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/1/1?curriculum_id=506324b3a7dffd00020bf661#


I figured I would combine my newly-aquired knowledge of isNan() to check if my imaginary user isn't fooling my programme by trying to enter a number when I ask for a color. When I use the code below though, it seems to skip to the 'default' case instead of logging the string I entered for isNan(color), what's going on here?

I realise that the 'default' case covers this example, but I'm still curious as to what's going on.


var color = prompt("What's your favorite primary color?","Type your favorite color here");

switch(color) {
  case 'red':
    console.log("Red's a good color!");
    break;
  case 'blue':
    console.log("That's my favorite color, too!");
    break;
  case  'yellow':
      console.log("Like sunshine!");
      break;
  case isNaN(color) === false:
    console.log("Numbers aren't colors!");
    break;  
  default:
    console.log("I don't think that's a primary color!");
}


#2

Add while loop before switch statement
var color = prompt("What's your favorite primary color?","Type your favorite color here");

while((isNaN(color)===false)){
color = prompt("What's your favorite primary color?","Type your favorite color here");
}

next add your switch statement

switch(expression) {
case n:
code block
break;
case n:
code block
break;
default:
default code block
}

@yoerigod


#3

Thanks for the reply, I believe that solution works but it doesn't really answer the question I had yet. Why is the case: isNan(color) === false case not showing when the code looks like above?

@rcodeman


#4

it'll not work
because -
when color='red'
"Red's a good color!"
when color=' blue'
That's my favorite color, too!"
But when color= isNaN(color) === false
its means color=false but so what?
case can't checking
you can use it too before switch statement

if((isNaN(color)===false)){
color="number";
}
then take a case into switch statement

case 'number':
console.log("Numbers aren't colors!");
break;


#5

Thanks for the explanation!


#6

I've gotten it to work using the if statement as well!

Both this case:

var color = prompt("What's your favorite primary color?","Type your favorite color here");

switch(color) {
  case 'red':
    console.log("Red's a good color!");
    break;
  case 'blue':
    console.log("That's my favorite color, too!");
    break;
  case  'yellow':
    console.log("Like sunshine!");
    break;
  default:
      if (isNaN(color) === false) {
    console.log("Numbers aren't colors!");
    }
    else {
        console.log("I don't think that's a primary color!");
    }
};

and this case work perfectly well:

var color = prompt("What's your favorite primary color?","Type your favorite color here");

if (isNaN(color)) {
    switch(color) {
        case 'red':
            console.log("Red's a good color!");
            break;
        case 'blue':
            console.log("That's my favorite color, too!");
            break;
        case  'yellow':
            console.log("Like sunshine!");
            break;
        default:
            console.log("I don't think that's a primary color!");
    }
}
else {
    console.log("Numbers aren't colors!");
};