Switch cases:using multiple possibilities in a case?


#1

Hi everyone,
First time posting, not really an error but something i'm curious about.
Is it possible to make a case with multiple possibilities.e.g. when the user types in a number between 1 and 3, one result is logged? i tried putting in 1 < answer < 3 in the case parentheses but it didnt work(Please explain as simply as possible as i'm a newbie :slight_smile:)
Thank you


var answer = prompt("Choose a number between 1 and 6");

switch(answer){
    case("3"):console.log("three");
    break;
    case("4"):console.log("four");
    break;
    case("6"):console.log("six");
    break;
    default:console.log("try again");
    break;
}


#2

You mean something like this:

var answer = prompt("Choose a number between 1 and 6");
switch (answer) {
  case "1":
  case "2":
  case "3":
    console.log("Number between 1-3");
    break;
  default:
    console.log("No number given");
}

Here you can see more Details/Examples


#3

thank you, i didn't realise it was so simple haha


#4

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