My Rock Paper Scissors Project

Here is my Rock Paper Scissors project.

const getUserChoice = userInput => { userInput = userInput.toLowerCase(); if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') { return userInput; } else { console.log('Invalid choice. Say rock, paper, or scissors.'); }; }; const getComputerChoice = () => { let randNum = Math.floor(Math.random() * 3); return randNum === 0 ? 'rock' : (randNum === 1 ? 'paper' : 'scissors'); }; const determineWinner = (userChoice, computerChoice) => { if(userChoice === 'bomb') { return 'You won'; } if(userChoice === computerChoice) { return 'The game ended in a tie'; }; if(userChoice === 'rock') { if(computerChoice === 'paper') { return 'You lost.'; } else { return 'You won'; }; }; if(userChoice === 'paper') { if(computerChoice === 'scissors') { return 'You lost'; } else { return 'You won'; }; }; if(userChoice === 'scissors') { if(computerChoice === 'rock') { return 'You lost'; } else { return 'You won'; }; }; }; const playGame = () => { let userChoice = getUserChoice('bomb'); let computerChoice = getComputerChoice(); console.log(`You played ${userChoice} and the computer played ${computerChoice}`); console.log(determineWinner(userChoice, computerChoice)); }; playGame();
5 Likes

Dear @stuffundefined, unknowingly you helped me when I tried to use the ternary conditional instead of the IF or Switch statements…
I was using return inside of the truthy / falsy expressions, and now I know why I was getting errors.
Thanks!!!

3 Likes

same here.
Thank you @stuffundefined
!

Hi! I have just started learning JS and I have a question. In the “determineWinner” function how does computer defies “userChoice” and “computerChoice”? Because in the functions above these two aren’t defined or mentioned. Does it somehow takes it from “getComputerChoise” and "getUserChoise’ omitting “get”? I’m really confused about this part of code. Thanks in advance

That’s about the arguments and parameters of functions.

The userChoice variable (in the playGame function) is used as the userChoice parameter in the getUserChoice.

Here’s an example of the sequence of stuff that happens.

function addOne(n) {
  return n + 1;
}

var x = 5;
var y = addOne(x);
console.log("y = " + y);

x is 5
and x is used as the first argument for calling the addOne function
so the value contained in x becomes the first parameter of the addOne function, which is n
so n is 5 during that function call.
The function returns n + 1 so the function returns 6
This result is put as variable y

Thank you so much for answering. Still a bit confused, but it’s getting clearer after your example. :heart: