|| in switch loops


#1

How come switch loops don't accept || in the cases but they accept &&?

var response= prompt ("What is you favorite season? Choose from fall, winter, spring or summer.");
switch(response){

case "fall": 
    console.log("leaves!");
    break;
case "winter":
    console.log("snow!");
    break;
case "spring" && "Spring":
    console.log("new life!");
    break;
case "summer":
    console.log("grilling out and swimming!");
    break;
default:
console.log ("there is something to enjoy about all the seasons");}

#2

Yeah it works here

var response= prompt ("What is you favorite season? Choose from fall, winter, spring or summer.");
switch(response){
case "fall"||"FALL": 
    console.log("leaves!");
    break;
case "winter"||"WINTER":
    console.log("snow!");
    break;
case "spring" && "Spring":
    console.log("new life!");
    break;
case "summer":
    console.log("grilling out and swimming!");
    break;
default:
console.log ("there is something to enjoy about all the seasons");}

the pit fall is this block

case "spring" && "Spring":
    console.log("new life!");
    break;

can never executed because no matter what the user enters it cannot be both hence it will evaluate to false


#3

Hi @laurenb2015

You can simplify it with .toUpperCase method. Add it like that:

var response= prompt ("What is you favorite season? Choose from fall, winter, spring or summer.").toUpperCase;

It'll change the response user in Upper Case automatically.

So now, switch look like:

switch(response){
case "FALL": 
    console.log("leaves!");
    break;
case "WINTER":
    console.log("snow!");
    break;
case "SPRING":
    console.log("new life!");
    break;
case "SUMMER":
    console.log("grilling out and swimming!");
    break;
default:
console.log ("there is something to enjoy about all the seasons");}

Hold on :wink: