Skip to content

Instantly share code, notes, and snippets.

@pedromcunha
Last active August 29, 2015 14:04
Show Gist options
  • Save pedromcunha/791c80f3ab800ccfb49f to your computer and use it in GitHub Desktop.
Save pedromcunha/791c80f3ab800ccfb49f to your computer and use it in GitHub Desktop.
If/Else Case Switch Conditionals
function sansTemple(floor){
var happensNext;
switch(floor) {
//Notice how you can use a stacking method to reduce redundency in cases. Now lower level also works for the basement cond.
case "lower level" :
case "basement" :
welcomeMessage = "Welcome to the basement!";
case 1 :
welcomeMessage = "You have entered the First Floor!";
break
case 2 :
welcomeMessage = "Sweet, made it to the Second Floor.";
break
case 3 :
welcomeMessage = "Feel free to rest on the Third Floor!";
break
case 4 :
welcomeMessage = "Damn, you've made it to the fourth floor!";
break
case 5 :
welcomeMessage = "Wow, you've made it all the way to the top floor. Not much to see here... or is there?";
break
default :
welcomeMessage = "You alright man? That's not a floor!";
}
return welcomeMessage;
}
sansTemple();
//Just a quick note, you can name cases anything that can be entered as a value (variable, number, string, expression)
@pedromcunha
Copy link
Author

Stacking the cases you can also use the fall through method to make the cases inherit the others case (don't use the break keyword to achieve this affect).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment