Created
December 24, 2020 20:48
-
-
Save oliverjumpertz/a9ef9386f6c99583ac66d4f5cd93c581 to your computer and use it in GitHub Desktop.
Switching over anything
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCheering(followers) { | |
// If you were to switch over followers, you'd be surprised, | |
// because you'd always hit the default case. | |
// By switching over true, all cases get evaluated properly, although they | |
// contain numeric ranges! | |
switch (true) { | |
case followers >= 2000: | |
return "Wow, I'm speechless. I'm so happy, thank you!!!"; | |
case followers >= 1000: | |
return "Wow, this is so great!"; | |
case followers >= 500: | |
return "Awesome!"; | |
case followers >= 100: | |
return "Wooooo!"; | |
default: | |
return "Yay!"; | |
} | |
} | |
const cheer = getCheering(2125); | |
console.log(cheer); // => "Wow, I'm speechless. I'm so happy, thank you!!!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment