JavaScript Rock Paper Scissors Game Help


#1

I am stuck on the rock paper scissors exercise.
https://www.codecademy.com/courses/learn-javascript-functions/projects/rock-paper-scissors?course_redirect=introduction-to-javascript

I keep receiving ‘undefined’ in the console and I can not figure out why.
/*
You threw: scissors
The computer threw: rock
undefined
*/

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

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

function determineWinner(getUserChoice, GetComputerChoice) {
if (getUserChoice === getComputerChoice) {
  return 'The game is a tie!';
 if (getUserChoice === 'rock') {
   if (getComputerChoice === 'paper') {
     return 'The computer won!';
   } else {
     return 'You Won!';
    
if (getUserChoice === 'paper') {
  if (getComputerChoice === 'scissors') {
    return 'The computer won!';
  } else {
    return 'You won!';
  	 }
 		}
 	 }
	}
 }
}
const playGame = () => {
    const userChoice = getUserChoice('scissors');
    const computerChoice = getComputerChoice();
    console.log('You threw: ' + userChoice);
    console.log('The computer threw: ' + computerChoice);
    console.log(determineWinner(getUserChoice, getComputerChoice));
}

playGame();

#2

your determineWinner function is faulty, you nested your conditions wrong. Just a small example:

if (getUserChoice === getComputerChoice) {
  return 'The game is a tie!';
 if (getUserChoice === 'rock')

if (getUserChoice === 'rock') condition is only reached when the result is a tie. what you should is check if the user choice/picked rock if the result is not a tie, which is done by closing if (getUserChoice === getComputerChoice) clause:

if (getUserChoice === getComputerChoice) {
  return 'The game is a tie!';
} // closing if tie clause!!!
 if (getUserChoice === 'rock')

your determineWinner function is full of this kind of examples, please walk through them to get the flow of your program right, good luck! :slight_smile:


#3

Ok, so here is the updated version.

  • I’ve changed the ‘getUserChoice’ and 'getComputerChoice
  • closed the if tie clause
  • went through the flow

Although I still receive the same error
/* You threw: scissors
The computer threw: rock
undefined
*/
Here is my updated code:

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

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

function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
  return 'The game is a tie!';
}// closing if tie clause!!!
  
 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!';
  	}
  }
}; 

const playGame = () => {
    let userChoice = getUserChoice('scissors');
    const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
    console.log('The computer threw: ' + computerChoice);
  		console.log(determineWinner(userChoice,computerChoice));
}
playGame();

#4

here:

 if (userChoice === 'rock') {
  if (computerChoice === 'paper') {
     return 'The computer won!';
  } else {
     return 'You Won!';
  } 
}; // unnecessary semi-colon

a closing curly bracket of if clause shouldn’t have a semi-colon

there is no if clause to check what happens when user picks scissors

ideally, you would use else if for:

  if (userChoice === 'rock')

and:

if (userChoice === 'paper')

given there is no point in checking if user picked paper if they already picked rock


#5

Success!

Here is what I did:

  • I took your advice on the else if statement
  • Added the either logical operator ||
  • Removed code since the logical operator took action.

This code works 100% i’ve tested for all possible attempts.
Thank you very much for the assist!

Below is the updated code:

function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
  return 'The game is a tie!';
} // closing if tie clause!!!
  
else if (userChoice === 'rock' || 'paper' || 'scissors') {
  if (computerChoice === 'paper') {
     return 'You won!';
  } else {
     return 'Computer Won!';
  } 
 }
}

const playGame = () => {
    let userChoice = getUserChoice('scissors');
    const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
    console.log('The computer threw: ' + computerChoice);
  		console.log(determineWinner(userChoice,computerChoice));
}
playGame();

#6

i wouldn’t call it success, the user never gets the option to pick rock, paper or scissors, why not use prompt() for this?

using the or operator (||) is a terrific idea :smiley: nope, sorry, absolute not. Simple proof of concept of how bad idea it is:

userChoice = 'rock'
computerChoice = 'paper'
if (userChoice === computerChoice) {
  console.log('The game is a tie!');
} // closing if tie clause!!!
  
else if (userChoice === 'rock' || 'paper' || 'scissors') {
  if (computerChoice === 'paper') {
     console.log('You won!');
  } else {
     console.log('Computer Won!');
  } 
 }

rock is beating paper, uhm… not good?


#7

You were 100% correct about the wrong use of the logical operator.

I’ve went back to the code I erased and improved it.

I’ve added more else if statements to the block and added the game rules appropriately.

Below is my updated code:

function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
  return 'The game is a tie!';
} // closing if tie clause!!!
  
else if (userChoice === 'rock') {
  if (computerChoice === 'paper') {
     return 'Computer Won!';
  } else {
     return 'You won!';
 } 
}

else if (userChoice === 'scissors') {
  if (computerChoice === 'rock') {
    return 'Computer won!';
  } else {
    return 'You won!';
  }
} 
else if (userChoice === 'paper') {
  if (computerChoice === 'scissors') {
    return 'Computer won!';
  } else {
    return 'You won!';
    
  }
 }
}

#8

that looks much better, nicely done :slight_smile:


#9

Thank you!
Just one more thing.
Where would I place prompt() I am having trouble using this function.


#10

in your getUserChoice, remove the parameter from the function and update prompt() for input and store it in userInput


#11

Ok, so I’ve replaced the parameter for prompt (1/2)

const playGame = () => {
    let userChoice = getUserChoice(prompt());
    const computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);
  console.log(determineWinner(userChoice,computerChoice));
}
playGame();

How would I store prompt() in userInput? (2/2)

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

#12

if you really want to keep the userInput parameter you should prompt first:

let userPrompt =  prompt("enter a choice: ");

then supply this as argument to the function:

let userChoice = getUserChoice(userPrompt);

#13

When I use these I receive a reference error that says prompt is not defined.


#14

My apologies by the way if I seem too needy. I’m new to programming and I’ve never used prompt() in JavaScript.
Any extra help is very appreciated :grin:


#15

its a buit in function:

should exist? Difficult to say without your code


#16

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