Switch statement syntax

If one wanted to return ‘rock’ for 0 then this is how the code would look.
If one wanted to return ‘rock’ for 3, would the first case still be 0 or would it be 3?

// Define a 'const' function for computer choice. 
const getComputerChoice = () => {

    const randomNumber = Math.floor(Math.random() * 3);
    
    switch (randomNumber) {

    case 0: 
        return "rock";
    case 1:
        return "paper";
    case 2: 
        return "scissors";
    
    }

};

Cases can be in any order, especially since you are returning from each branch.

1 Like