Rock, Paper, Scissor Game

Hello everyone, I am new to learning JavaScript after Python (Don’t know if wrong or right here but the fact is that I am enjoying it more than Python because I can understand more and better now), my question in this exercise or my problem would the following:

  1. User always get rock… why? Is it because the first IF is always true so ignores the other conditions?

  2. The bomb! condition never happens because of number one and I don’t think I wrote it properly.

  3. I am confused about the arrow function. Is it the only way now that we can declare a function, at this point it seems like it is and that it’s the best way to it.

I am attaching the exercise and hope to hear from you soon so I can move on :smiley:

Rock, Paper Scissors.

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' ||  userInput === 'scissors' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Wrong guess, try again!');
  }
};

const getComputerChoice = () => {
  const randomNumber = Math.floor(Math.random() * 3);
  if (randomNumber === 0) {
    return 'rock';
  } else if (randomNumber === 1) {
    return 'paper';
  } else if (randomNumber === 2) {
    return 'scissors';
  }
};
  
const determinateWinner = (userChoice, computerChoice) => {
  if ( userChoice === computerChoice) {
    return 'The game is a tie!'
  } if (userChoice === 'rock') {
    if (computerChoice === 'paper')
      return 'The computer won!'; 
  } else {
    return 'You won!';
  } if (userChoice === 'paper') {
    if (computerChoice === 'scissors') 
      return 'The computer won!';
  } else {
    return 'You won!';
  } if (userChoice === 'scissors') {
    if (computerChoice === 'paper')
      return 'You won!';
  } else {
    return 'The computer won!';
  } if (userChoice === 'bomb' ) {
    if (computerChoice === randomNumber)
    return 'You won!!!'
  }
};

const playGame = () => {
  const userChoice = getUserChoice('rock');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer thew: ' + computerChoice);
  console.log(determinateWinner(userChoice, computerChoice));
};

playGame();

no, because you hard-coded the user choice string:

const userChoice = getUserChoice('rock');

you could prompt the user for input?

as for point 2, here is your code with different indent:

if (userChoice === 'rock') {
  if (computerChoice === 'paper')
      return 'The computer won!'; 
} else {
  return 'You won!';
}

as you can see, the else belongs to the first if condition, in other words, if the user doesn’t choice rock, they won. You forgot the { here:

  if (computerChoice === 'paper') // missing {

i think you need to walk through the flow of your program.

its not, as mentioned in the documentation:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

THANK YOU.

So number 2, each if needs an {} and the indentation is also wrong right?

This way I can’t prompt the user for an input (I haven’t learned that yet on this course), but if I do I’d need to create a function or just add a new line of code under getUserChoice and store it as userInput? (I am just throwing ideas here).

Thank you also for the documentation I will read it!

indention is not vital (it won’t throw syntax errors) but using nice indention makes your code and the flow a lot more readable

not sure its that simple, you get a bunch of if statement with the user choice:

// over simplified not really code
if (userchoice === paper){}
if (userchoice === rock){}
if (userchoice === scissors){}

within each of this if statements, you get an if and else checking the computer choice and determining the winner

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.