More on control flow in JS // 'all on your own', write your own Switch


Hi guys,

So I've managed to get the Switch exercises to work when using a prompt to collect Strings as answers. I thought I would try to be a bit more clever and try to collect numbers as answers. However, when I do this, it doesn't work. See my code below.

Any help appreciated!

Thanks :slight_smile:

var height = prompt("How tall are you?", "Enter your height in cm");

switch(height) {
    case '>180':
        console.log("Wowza, that's tall");
    case '>160':
        console.log("Average height");
    case ' <=160':
        console.log("that's not a height");

So the prompt works fine.

But when I enter any number, or any content, e.g. a string etc, it triggers the default. E.g. if I enter 190, I would expect it to trigger the first case and to print out "Wowza, that's tall!".

I am not sure what is wrong.

I have tried changing the '' around the case, e.g. from '>180' to (>180) and even 'height >180' and (height>180) but none of those seem to work. I even removed the ' entirely, and just had case > 180 , but that didn't help either.


Hi @terarockstar74318,

I tried to do this with the FizzBuzz exercise and learned... that (I'm pretty sure) you can't really use boolean expressions as cases (in the current iteration of your program I believe they wouldn't be such anyway because they are in quotes --> strings)... at least not in this way. (I've thought about adding an additional if statement to sort answers into my cases, but then really, I might as well just use the if statement I think.)


(PS: Although there does seem to be some type of workaround. But it is a workaround.)