I'm just testing and wondering if this could work? (Switch statement)


#1

It works but not my way. How to change it? Is it possible to assign my variable "WPA" into case?

var answer = prompt("What is you favourite MS Office program? Word, Excel, PowerPoint or Access ?")
WPA = ("Word" || "PowerPoint" || "Access")

switch(answer){
case (WPA):
console.log("Good joke :D");
break;
default:
console.log("Excel is OK")
break;
}


#2

You mean like this?

var a = prompt("Pick a, b, or c");

switch(a) {
case "a":
    var answer = "Alpha";
    break;
case "b":
    var answer = "Bravo";
    break;
case "c":
    var answer = "Charlie";
    break;
}

#3

TY for replay. I was wondering if my code could work as yours. So instead of using 3times "case" i could just use in one 'case' a variable that contain three different strings.


#4

You cannot assign it to three as you did here:

But you could assign it to two, using the ternary operator.


#5

@tolmin

Look here

You forgot to put a semicolon.

Huh, what is this exactly? P.-S. A semicolon is missing!

A semicolon is missing here too.

No need to put a break after a default case.

I don't think this will work. You should try putting your case statement like this:

case "firstCase":
//We do something in here
break;
case "secondCase":
//We do something in here
break;
case "thirdCase":
//blablabla you know what I mean

Hope this helped you.


#6

Thanks! I will look at this operator.


#7

@miniapple8888
There is no need to help with syntax,
he was just writing a small example of
his thoughts on assignment.


#8

Too late, but it's possible to do something like this:

var answer = prompt("What is you favourite MS Office program? Word, Excel, PowerPoint or Access ?")

switch(answer) {
case 'Word':
case 'PowerPoint':
case 'Access':
    console.log("Good joke :D");
    break;

default:
    console.log("Excel is OK")
    break;

}


#9

@pechera

No need of break; for default block.


#10

yes you can have multiple case's printing out one output.


#11