Rock Paper Scissors issue

Hi,
I keep getting a syntax Error : Unexpected Identifier problem in my code, and I can not seem to figure it out. Is there a helpful person out there who might see where I went wrong? :slightly_smiling_face:

// Userinput of rock, paper or scissors.
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’) {
return userInput;
} else {
console.log(‘Error!’);
}
};

//Computer randomized selection.
function getComputerChoice(){
const randomNumber =
Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 2:
return ‘scissors’;
}
}
//Winner determiner
funtion determineWinner(getUserChoice, getComputerChoice){
if(getUserChoice === getComputerChoice){
return ‘The game is a tie!’;
}

if(getUserchoice === ‘rock’){
if(getComputerChoice === ‘paper’){
return ‘Computer Won!’;
} else {
return ‘User Won!’;
}
}
if(getUserchoice === ‘paper’){
if(getComputerChoice === ‘scissors’){
return ‘Computer Won!’;
} else {
return ‘User Won!’;
}
}
if(getUserchoice === ‘scissors’){
if(getComputerChoice === ‘rock’){
return ‘Computer Won!’;
} else {
return ‘User Won!’;
}
}
}
console.log(determineWinner(‘paper’, ‘scissors’));

3 Likes

You don’t have to assign it to itself because it’s mutating which means you don’t have to assign it to itself to save it.

You used three closing brackets instead of two.
And in the determineWinner function you used if’s after the first if, you should use else if
So the whole code:

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

//Computer randomized selection.
function getComputerChoice(){
  const randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
};
//Winner determiner
function determineWinner(getUserChoice, getComputerChoice){
  if(getUserChoice === getComputerChoice){
    return 'The game is a tie!';
  } else if(getUserchoice === 'rock'){
    if(getComputerChoice === 'paper'){
      return 'Computer Won!';
    } else {
      return 'User Won!';
    }
  } else if(getUserchoice === 'paper'){
    if(getComputerChoice === 'scissors'){
      return 'Computer Won!';
    } else {
       return 'User Won!';
    }
  } else if(getUserchoice === ‘scissors’){
    if(getComputerChoice === ‘rock’){
      return 'Computer Won!';
    } else {
      return 'User Won!';
    }
  }
}

console.log(determineWinner(getUserChoice('paper'), getComputerChoice()));
3 Likes

You might want to add some break; statements for each case in the switch. Also your if statements are a bit messy, you can write it all in one if else statement and using conditionals. This is how I did it:

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'It\'s a tie!';
  } else if (userChoice === 'rock' && computerChoice === 'paper') {
    return 'You Lose! The computer won!';
  } else if (userChoice === 'paper' && computerChoice === 'scissors') {
    return 'You Lose! The computer won!';
  } else if (userChoice === 'scissors' && computerChoice === 'rock') {
    return 'You Lose! The computer won!';   
  } else {
    return 'Winner Winner Chicken Dinner! You won!'
  };
};
3 Likes

You missed a c there :wink: Function

3 Likes

Thank you lolfail, I got past the error with that, and now just have to figure out why getUserChoice is not defined when I test the logic.

JavaScript is hard :smile:

3 Likes

Yeah, just found the error… I had not referenced the correct function, since i wrote getUserchoice instead of getUserChoice… :slight_smile:

3 Likes