Stuck on the final step of "Rock paper scissors" project

Hello and thanks in advance!

I am on the last step of the “Rock paper scissors” project and I can’t figure out why
playGame();
won’t work. I tested out all the other steps and everything seems to work correctly right up to that last point. I will paste my code below:

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

const getComputerChoice = () => {
  const randomNumber = 
console.log = Math.floor(Math.random() * 3);
switch (randomNumber) {
  case 0:
    return 'rock';
  case 1:
    return 'paper';
  case 2:
    return 'scissors';
    }
};

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
  return 'The game is a tie!';
}
if (userChoice === 'rock') {
  if(computerChoice === 'paper') {
    return 'I won!';
} else {
  return 'You won';
}
}
if (userChoice === 'paper') {
  if(computerChoice === 'scissors') {
    return 'I won!';
} else {
    return 'You won!';
}
}
if (userChoice === 'scissors') {
  if (computerChoice === 'rock') {
    return 'I won!';
  } else {
    return 'You Won!';
  }
}
};

  const playGame = () => {
    const userChoice = getUserChoice('rock');
    const computerChoice = getComputerChoice();
    console.log('You threw: ' + userChoice);
    console.log('I threw: ' + computerChoice);
    console.log(determineWinner(userChoice, computerChoice));
};

playGame();

Thanks again for any help!

Hello! In the first function, there are a few errors. Firstly, count the number of opening braces { on lines 1-9, and the number of closing braces } on the same lines. Next, and more importantly, look at how you define the userInput function. Defining a function using arrow syntax uses the following template:

const someFunc = (param1, param2) => {
//code
}

Now look at yours again. See a difference?

Thirdly, in playGame, you call a function called getUserChoice(). Where have you defined this function in the code?

1 Like

Sorry, I just noticed that for some reason it didn’t copy my whole code.
Here it is:

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

const getComputerChoice = () => {
  const randomNumber = 
console.log = Math.floor(Math.random() * 3);
switch (randomNumber) {
  case 0:
    return 'rock';
  case 1:
    return 'paper';
  case 2:
    return 'scissors';
    }
};

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
  return 'The game is a tie!';
}
if (userChoice === 'rock') {
  if(computerChoice === 'paper') {
    return 'I won!';
} else {
  return 'You won';
}
}
if (userChoice === 'paper') {
  if(computerChoice === 'scissors') {
    return 'I won!';
} else {
    return 'You won!';
}
}
if (userChoice === 'scissors') {
  if (computerChoice === 'rock') {
    return 'I won!';
  } else {
    return 'You Won!';
  }
}
};

  const playGame = () => {
    const userChoice = getUserChoice('rock');
    const computerChoice = getComputerChoice();
    console.log('You threw: ' + userChoice);
    console.log('I threw: ' + computerChoice);
    console.log(determineWinner(userChoice, computerChoice));
};

playGame();

I assume the issue is with the playGame function since the whole thing worked perfectly right up to that step.

Oh, ok. That makes sense. What’s wrong with it? Are there any errors being thrown?

This is what the console spits out when I try to call “playGame()”

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:54
console.log('You threw: ’ + userChoice);
^

TypeError: console.log is not a function
at playGame (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:54:13)
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:59:1)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

Ah. Try copy all of your code, and refreshing your browser. I can’t seem to find any reason for this error. @selectall, any insights?

1 Like

Looks like there’s an issue in the code block here:

const getComputerChoice = () => {
  const randomNumber = 
console.log = Math.floor(Math.random() * 3);
switch (randomNumber) {
  case 0:
    return 'rock';
  case 1:
    return 'paper';
  case 2:
    return 'scissors';
    }
};

This is one that’s very difficult to spot. console.log is being assigned a new value, so it can’t be used to log to the console anymore.

Huzzah! This was it! I had an extra ‘console.log’ in there where it shouldn’t be.
Here is the fix that worked fine!

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

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

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
  return 'The game is a tie!';
}
if (userChoice === 'rock') {
  if(computerChoice === 'paper') {
    return 'I won!';
} else {
  return 'You won';
}
}
if (userChoice === 'paper') {
  if(computerChoice === 'scissors') {
    return 'I won!';
} else {
    return 'You won!';
}
}
if (userChoice === 'scissors') {
  if (computerChoice === 'rock') {
    return 'I won!';
  } else {
     return 'You won!';
  }
}
  if (userChoice === 'bomb'){
    return 'You went ultra instinct!!'
  }
};

  const playGame = () => {
    const userChoice = getUserChoice('bomb');
    const computerChoice = getComputerChoice();
    console.log('You threw: ' + userChoice);
    console.log('I threw: ' + computerChoice);
    console.log(determineWinner(userChoice, computerChoice));
};

playGame();

Thanks again for all the help!

2 Likes