Rock, Paper, Scissors


#1

Hey guys,
I am working on Rock, Paper, Scissors from Learn Javascript. I researched on the forum and there is no code like mine. I think my lessons are different from the other users.
There are a few steps to execute the code, but unfortunately, it does not work. The Hints in each step are not that useful, as it is easy to research in another site then find the answer on codecademy. However, I could not make this work.
Does somebody know what is wrong with this code?
It should print the winner, however, the only answer printed is “the game is a tie”.
Here is the code. I am on step 11 btw.

const getUserChoice = (userInput) => {
	userInput = userInput.toLowerCase();
	if (userInput === 'paper' || userInput === 'rock' || userInput === 'scissors') {
  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 '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';
        }
       }
  };

console.log(determineWinner('paper', 'scissors'));
console.log(determineWinner('paper', 'paper'));
console.log(determineWinner('paper', 'rock'));

Thanks


#2

unnecessary semi-colon can cause interrupts.

like on this line:

if (userChoice === computerChoice); 

that will prevent the code from working properly

Basically, an if-else must be followed by either a statement or a group of statements.

if-else followed by a statement:

if (condition) statement;
if (condition); // followed by a statement (an empty statement)

#3

Hey Stetim.

Thank you for the tip. You are right. Now the code is working.
Do you know how can I identify mistakes like this myself? I lost 2 hours yesterday trying to fix it and then I gave up and almost decided to don’t go ahead with the course.
Thank to you I am back to the track. But I would like to understand what are the mistakes and how to identify. Is that possible?
Thanks


#4

That is a good question, i wish i had a good answer.

maybe a debugger helps? Debuggers are very useful tools, surely one must exist for javascript and is findable on google

well, the if clause always ran, something was wrong with if condition. Don’t just try things, understand things, that makes it easier to find bugs.

Keep practicing, you will get better at catching errors


#6

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