 # Rock, Paper, Scissors Activity - If/Else vs Switch

Hello! I’m worked through the Rock, Paper, Scissors activity and I wanted to check to see if I’m doing this right. In Step 5 you can create either an if/else or switch statement. It guides you through making the switch statement but I want to make sure I’m doing the if/else statement correctly as well. Also when would I choose to use a switch statement vs an if/else statement?

Here’s my attempt:

``````const getComputerChoice = () => {
const randomNumber = Math.floor (Math.random() * 3)
if (randomNumber === 0) {
console.log ('The computer went for rock');
}
else if (randomNumber === 1) {
console.log  ('The computer went for paper');
}
else if (randomNumber === 2) {
console.log ('The computer went for scissors');
}
};
//console.log (getComputerChoice());

const getComputerChoice = () => {
const randomNumber = Math.floor (Math.random() * 3);
switch (randomNumber) {
case 0:
return 'The computer went for rock';
case 1:
return 'The computer went for paper';
case 2:
return 'The computer went for scissors';
}
};
``````

Your `if..else if` statements are fine, but typically you’d end such a structure with an `else`. There are only 3 possible values (0, 1, 2), so do you need `else if (randomNumber === 2)`?

That’s entirely up to you. Some would say that `switch` is better when there are many options, but others would argue that a series of `if` statements works just as well.
For example:

``````const getComputerChoice = () => {
const randomNumber = Math.floor (Math.random() * 3)
if (randomNumber === 0) return 'The computer went for rock'
if (randomNumber === 1) return 'The computer went for paper'
return 'The computer went for scissors' //if neither of the previous return statements are executed this one will be, so it is in effect the default
};
//versus:
const getComputerChoice = () => {
const randomNumber = Math.floor (Math.random() * 3);
switch (randomNumber) {
case 0:
return 'The computer went for rock';
case 1:
return 'The computer went for paper';
case 2: //could also simply be default:, but makes no difference
return 'The computer went for scissors';
}
};
//versus:
const getComputerChoice = () => {
const randomNumber = Math.floor (Math.random() * 3)
if (randomNumber === 0) {
return 'The computer went for rock';
} else if (randomNumber === 1) {
return 'The computer went for paper';
} else {
return 'The computer went for scissors';
}
};
``````

Three ways to accomplish the same task.

There’s also the ternary expression with which you may not be familiar with yet:

``````const getComputerChoice = () => {
const randomNumber = Math.floor(Math
.random() * 3)
return randomNumber === 0 ?
'The computer went for rock' :
randomNumber === 1 ?
'The computer went for paper' :
'The computer went for scissors'
};
//the return can be all on a single line:
//return randomNumber === 0 ? 'The computer went for rock' : randomNumber === 1 ? 'The computer went for paper' : 'The computer went for scissors'
``````
1 Like

Two thoughts:

Your first attempt does not return anything, yet the next function `determineWinner()` expects a variable with the value of the choice made by the computer. It does however log good information in the form of a string.

The second attempt does return something, a whole string, but this might be less convenient to apply in the `determineWinner` function.

I would personally, just return the choice i.e. ‘sciccors’, but before that, log the sentence like so:

``````case 0:
console.log('The computer went for rock')
return 'rock';
``````

Happy coding!