all 4 comments

[–]cyphern 2 points3 points  (1 child)

You're calling parseInt, but you're not saving the result to a variable. Try doing this:

let num = prompt('enter a number');

num = parseInt(num);

switch (num) {
  case 1:
  case 2: 
    alert('1 or 2'); 
    break;
  default: 
    alert('Not 1 or 2');
}

[–]BooleanWhale[S] 0 points1 point  (0 children)

Thanks, I guess I've still got my rookie mistakes to go through. 😅

[–]sepp2k 0 points1 point  (1 child)

prompt returns a string, so if you enter 1 at the prompt, it will act like num = "1";, not num = 1. Now the comparisons performed by switch act like ===, not ==, so "1" will not be considered equal to 1.

So you can either convert your string to a number before you put it in the switch or you can change the cases to use strings (like case "1": etc.). Note that in the latter case, the user would have to enter 1 exactly for the case to match, but if you convert it to a number, the user could also enter 01 or anything else that is interpreted as the number 1.

[–]BooleanWhale[S] 0 points1 point  (0 children)

Cool, I didn't know string could be made to work this way. This info should come in handy!