Rock, Paper, Scissors Project

Hi everyone I’m just needing help with the code I created for the rock paper scissors project. I don’t have pro so I winged it but here is my code. It works but I feel it is pretty inefficient and I could have utilized functions more to simplify the process. Are there any suggestions on how to go about doing a project like this more simply? Here is my code.

const computerAnswer = Math.ceil(Math.random() * 3);

const userAnswer = ‘Rock’

console.log(You picked ${userAnswer});

switch (computerAnswer) {

case 1 :

console.log(‘Computer Picked Rock’);

break;

case 2 :

console.log(‘Computer Picked Paper’);

break;

default :

console.log(‘Computer Picked Scissors’);

break;

}

function getWinner() {

if (computerAnswer === 1) {

return “It’s a tie!”;

} else if (userAnswer === ‘Rock’ && computerAnswer === 2) {

return ‘You Lose!’;

} else if (userAnswer === ‘Rock’ && computerAnswer === 3) {

return ‘You Win!’;

} else if (userAnswer === ‘Paper’ && computerAnswer === 1) {

return ‘You Win!’;

} else if (userAnswer === ‘Paper’ && computerAnswer === 3) {

return ‘You Lose!’;

} else if (userAnswer === ‘Scissors’ && computerAnswer === 1) {

return ‘You Lose!’;

} else {

return ‘You Win!’;

}

}

console.log(getWinner());

Hello @starkiller18120. As you said, having functions around related code would be useful. Also, in this switch, you are effectively wasting code-you have the logic, but when you check for the winner, you use numbers:

The you saved the result of the switch/case statements in a variable-or returned the result from a function, you could have computerAnswer === 'Paper', which would make a bit more sense. Other than those two, I would have done this project in a very similar way.

I hope this helps!

1 Like

Also for future reference, check this out How Do I Format Code In My Post?

1 Like