Rock Paper Scissors Project

I tried to use switch over if else to complete this project. This is my code so far below, however it shows an error. Hopefully someone can point out why and how I’d write this correctly. Thanks :slight_smile:

const getUserChoice = userInput => {
userInput = userInput.lowercase();
switch userInput {
case ‘rock’:
return;
break;
case ‘paper’:
return;
break;
case ‘scissors’:
return;
break;
default:
console.log(Invalid Entry);

}

}

console.log(getUserChoice(test));

Hi there!. This article (How Do I Format Code In My Post) might help you share your code in the future in the Forums.

To your question: I see that you have return statements for each case but you aren’t returning anything. I think you want to try to do something like:

case 'rock':
  return 'rock';
  break;

Also, I noticed that in your default case inside the switch, you have console.log(Invalid Entry); The Invalid Entry part should be inside quotes since it’s a string.

Lastly, when you call your function at the end console.log(getUserChoice(test)); have you defined test anywhere? Because you cannot pass an argument to your function if it is undefined. Unless you were trying to use the string 'test', in which case you should put it inside quotation marks.

const getUserChoice = userInput => {
    userInput = userInput.toLowerCase();
    switch userInput {
        case 'rock':
        return 'rock';
        break;
        case 'paper':
        return 'paper';
        break;
        case 'scissors':
        return 'scissors';
        break;
        default: 
        console.log('Invalid Entry');

    }
}

console.log(getUserChoice('test'));

Thank you. I’ve adjusted as per your suggestions. Error is still there though. Looks to be with the switch userInput specifically looking at the error. It works if I use an if else statement, but I’m wanting to understand how switch should work in this scenario.

it might be the syntax. Have you tried: switch (userInput) { . I wasn’t sure about the parenthesis, but they might be mandatory to make the syntax work?

That was it!

Thank you!

1 Like

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput===‘rock’ || userInput ===‘paper’|| userInput===‘scissors’) {
return userInput ;
} else {
return ‘Error’;
}
}

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput===‘rock’ || userInput ===‘paper’|| userInput===‘scissors’) {
return userInput ;
} else {
return ‘Error’;
}
}

as far as I know, if a function has only one parameter it is not necessary to use parenthesis but in this code snippet, it seems like parenthesis is mandatory.can anyone explain why?

Do you mean it is mandatory for the lesson to give you a green checkmark, or for the code to be valid JavaScript? It’s important to keep in mind that sometimes the tests that Codecademy runs to check your work can be very picky and specific.