Share runnable code, everywhere.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/** 
 *  Sometimes you need to react based on a number. If statements
 *  are ok for that but a bit messy. Switches can be simpler and
 *  easier to add-to if you need to modify your code.
 **/
// Lets create a random number between 1 and 10 - use that as user-input
let userInput = Math.floor(Math.random() * 10) + 1;
switch (userInput) {
    case 1:
        console.log("You got the first number!");
        break;
    case 2:
        userInput = userInput + Math.floor(Math.random() * 100) + 1;
        console.log(`I generated a new random number 1-100: ${userInput}`);
        break;
    case 3:
        console.log("Three is such a beautiful prime number.");
        break;
    case 7:
        console.log("Seven is also a nice prime number.");
        break;
    case 9:
        console.log("9 is divisible by 3. True story, bro.");
        break;
        
    default:
        console.log(`The number generated was: ${userInput}`);
}
Enter to Rename, Shift+Enter to Preview