Rock, Paper, Scissors confusion


#1

https://www.codecademy.com/en/courses/learn-javascript/projects/rock-paper-scissors

I am not receiving any errors at all. I run the code and it plays the game. The problem is the answers it’s returning are incorrect.

So, I can run a game and the outcome will be:

You threw rock
The computer threw paper
The game was a tie.

I was under the assumption that the below statement would hold true through out the function determineWinner:

if (userChoice === computerChoice)
return ‘the game was a tie’

I’m also having games that end with the result:

You threw rock
The computer threw paper
The computer has won.

I tried discussing at great length with an online advisor via chat, but he wasn’t able to come up with anything.

Any thought/advice/answers are super appreciated.


var userInput = 'rock';
  userInput = userInput.toLowerCase();

function getComputerChoice() {
  Math.floor(Math.random() * 3);
  var randomNumber = Math.floor(Math.random() * 3);
  switch (randomNumber) {
    case 0:
      return 'rock';
      break;
    case 1:
      return 'paper';
      break;
    case 2:
      return 'scissors';
      break;
                     }        
}  
 
function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'The game was a tie.';
   }

  else if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'The computer has won';
    }  else {
       return 'You have won!'
    }
  } 
   else if (userChoice === 'paper') {
     if (computerChoice === 'scissors') {
       return 'The computer has won'
     }  else {
         return 'You have won!'
     }
   } 
    else if (userChoice === 'scissors') {
      if (computerChoice === 'paper') {
        return 'You have won!'
     }  else { return 'The computer has won'
             }
   } 
}
function playGame() {
  var userChoice = userInput;
  var computerChoice = getComputerChoice();
 console.log('You threw ' + userInput);
 console.log('The computer threw ' + getComputerChoice());
  
console.log(determineWinner(userChoice, computerChoice));  
}

playGame();

#2

the problem is here:

  var userChoice = userInput;
  var computerChoice = getComputerChoice();
 console.log('You threw ' + userInput);
 console.log('The computer threw ' + getComputerChoice());

calling the function for a second time here:

 console.log('The computer threw ' + getComputerChoice());

will result in a new pick by the computer, which could be anything. You should log the computerChoice variable to the console, so the computer picks one time, not two times


#3

Ah—That’s totally right and completely fixed it!!

I knew my logic was correct, just ended up calling the wrong dang variable.

Thanks so much!!


#4

Yep, now do you see how this goes wrong? computerChoice which you pass as argument determineWinner has a value you don’t know, because you log a different one to the console, which causes confusen. Good its all sorted now :slight_smile:


#5

Yeah, that makes total sense. I was giving them the extra roll with getComputerChoice, which had already been assigned once with computerChoice.

Thanks again!


#6

@stetim94, thanks for editing the code into Preformatted Text. It makes things so much easier. :smiley:

I was copying the codes into CC lab, turned out to be “Illegal Token”, when run the code, SyntaxError: Invalid or unexpected token. Even tried with Chrome console, same error.

After spending so much time figuring what is wrong, checking the code again and again, I only realized copying code without the Preformatted Text format will induce error, taught me a valuable lesson that the error is what I copied from this forum, all the strings turn out not to be strings after copied even with the quotation marks ’ '.

:tired_face:


#7

yes, if no format is applied the apostrophes and quotation marks get messed up. Which is pretty new, it wasn’t this way last week.


#8

@stetim94, something to do with Discourse? Was wondering about that as well, I used to copy those codes with no problem.

Well, at least I learnt something and what to look at if I encounter another “Illegal Token”, took me some readings and learnt that you can have Invisible character and cause some problem in the code.

Never knew that. Thought I was sharing the same problem, but in actual fact not. I have to manually change all those strings until I saw you edited the post. :joy:


#9

yep, discourse update.

Its also good to have had this error once, so you are familiar with it


#10

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