Rock, Paper, Scissor Project (JavaScript) Cannot correctly Determin Winner

the project I have difficulties with is:
https://www.codecademy.com/courses/learn-javascript/projects/rock-paper-scissors-javascript-deprecated

I have followed the YouTube tutorial of building “Rock, Paper, Scissors game in JavaScript” and wrote the following code. I was able to get a user choice and a computer choice but the result of derterminWinner is not matching with the user choice and computer choice in the console.log. I cannot find what went wrong.

Could anyone please give me a hint?


function  getUserChoice(userInput) {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb'){
    return userInput;
  } 
  else {
    console.log('Your choice is invalid!')
  }
};

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

function determinWinner(userChoice,computerChoice){
  var computerChoice = getComputerChoice()
  if (userChoice === computerChoice){
    return 'The game is a tie!';
  } else{
  if(userChoice === 'rock'){
    if(computerChoice === 'paper'){
      return 'Computer won!';
    }
    else {
      return 'User won!'
    }
  }
  
  if(userChoice === 'paper'){
    if(computerChoice === 'scissors'){
      return 'Computer won!';
    }
    else {
      return 'User won!'
    }
  }
  
    if(userChoice === 'scissors'){
    if(computerChoice === 'rock'){
      return 'Computer won!';
    }
    else {
      return 'User won!'
    }
  }
    if(userChoice === 'bomb'){
      return 'You won with the secret WEAPON!!!';
  }  
 }
}

function playGame(){
  var userChoice = getUserChoice('Paper');
  var computerChoice = getComputerChoice();
  console.log('Your choice is '+ userChoice +'.');
  console.log('The computer threw ' + computerChoice +'.');
  console.log(determinWinner(userChoice,computerChoice));
}

playGame()

Here is an example of the result. Link (evernote link to share a screenshot)

The issue is that you pass an argument to your determinWinner() function that is assigned to the computerChoice parameter. Then inside the function, you overwrite that value by reassigning a new value to computerChoice by calling the getComputerChoice() function again. The value you print to the console is the original value, but the winner is being decided based on a new value that you never see, so it may not be the same. This is producing your inconsistent results.

1 Like

Thank you so much! I have deleted the line in question and my code works! This is the very first long code that I wrote and I am so happy that it works!! (I tried 7 times for 2 weeks and I finally got it working with your help!)

Thank you! You are super!

3 Likes

You’re very welcome!