Rock, Paper, Scissors Project

I’m sure this has been beaten to death, but even after looking at many different examples I can’t understand why the game won’t run. I have even copied and pasted bits of code into the sections I thought had an issue to no avail.

Can anyone help? Thank you.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' ) {
  return userInput;
} else {
  console.log('Invalid Input')
  };
}

function getComputerChoice() {
  switch(Math.floor(Math.random()*3)) {
    case 0:
    return 'rock';
    case 1: 
    return 'paper';
    case 2: 
    return 'scissors'
  }
};

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
  return 'The game was 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 === 'rock'){
    return 'The Computer Won!'
  } else {
    return 'You Won!';
  };
  
const playGame = () => {
    let userChoice = getUserChoice('scissors');
    const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
    console.log('The computer threw: ' + computerChoice);
  		console.log(determineWinner(userChoice,computerChoice));
}

playGame();

1 Like

What error do you get?

the problem is here:

if (userChoice === 'scissors'){

the if has no matching closing curly bracket (}), and neither does your determineWinner function.

Using good indention for your code helps you with this kind of error

1 Like

Wonderful. Thank you. Got the brackets issue fixed (I think) though I am now throwing this error code
`/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:26
if (userChoice === ‘rock’) {
^

ReferenceError: userChoice is not defined
at Object. `

Can I see an updated version of your code?

Here you are. Thank you so much.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' ) {
  return userInput;
} else {
  console.log('Invalid Input')
  };
}

function getComputerChoice() {
  switch(Math.floor(Math.random()*3)) {
    case 0:
    return 'rock';
    case 1: 
    return 'paper';
    case 2: 
    return 'scissors'
  }
};

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
  return 'The game was 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 === 'rock'){
    return 'The Computer Won!'
  } else {
    return 'You Won!';
  }
}; 

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

playGame();

you closed your function, but a bit early, this is now the entire function:

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
  return 'The game was a tie!';
  }
} 

shouldn’t the other comparisons be part of the function as well?

Could you point out for me how the comparisons aren’t included? I looked at someone else’s solution and can’t see any differences. It still won’t run and the error code is coming up on
if (userChoice === ‘rock’)

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
  return 'The game was 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 === 'rock'){
    return 'The Computer Won!'
  } else {
    return 'You Won!'
  }
}; 

Hello!
See how many closing braces (}) there are after the first if block in the determineWinner function. Can you find an opening brace({) for each one? If so, should you have more, less, or keep the amount of closing braces here?