# Rock, Paper, Scissors Problem

I have been really struggling with this exercise. Now I have managed to get the code to spit out the choices of myself and the computer, but often I receive an undefined, when I want to call the winner.

Example:

I throw rock
Comp throws paper
undefined

So here must be a problem with the else statement.

I also always get undefined when I try to throw bomb. So there must be a problem in the determineWinner function, but it looks okay to me.

const getUserChoice = userInput => {

userInput = userInput.toLowerCase();

if (userInput === 'rock') {

return userInput

} if (userInput === 'paper') {

return userInput

} if (userInput === 'scissors') {

return userInput

} if (userInput === 'bomb') {

return userInput

} else {

console.log ('Wrong user input.');

}

} ;

//console.log(getUserChoice('banana'));

const getComputerChoice = () => {

const randomNumber = Math.floor(Math.random() * 3);

if (randomNumber === 0) {

return 'rock';

} if (randomNumber === 1) {

return 'paper';

} if (randomNumber === 2) {

return 'scissors';

}

};

//console.log(getComputerChoice());

const determineWinner = (userChoice, computerChoice) => {

if (userChoice === computerChoice) {

return 'This is a tie.'

if (userChoice === 'rock') {

if (computerChoice === 'paper') {

return 'The computer won!'

} else {

return 'You lost'

}

} if (userChoice === 'paper') {

if (computerChoice ==='scissors') {

return 'The computer wins!'

} else {

return 'You won!'

}

} if (userChoice === 'scissors') {

if (computerChoice === 'paper') {

return 'You win!'

} else {

return 'The computer wins!'

}

} if (userChoice === 'bomb') {

return 'You win big time!'

}

}

};

const playGame = () => {

const userChoice = getUserChoice('bomb');

const computerChoice = getComputerChoice();

console.log('You threw ' + userChoice);

console.log('The computer threw: ' + computerChoice);

console.log(determineWinner(userChoice, computerChoice));

};

playGame();

Could someone please have a look and tell me what I am doing wrong? Thank you.

Notice how the first if block in your determineWinner() function isn’t closed?

if (condition){
//code
}//this if block is closed

if (condition){
//code

//this one isn't

See how my second example is missing a closing bracket (})? Check your if block.

2 Likes

Thank you @codeneutrino. That was the problem. Any tips on how to prevent this in the future? I feel like I am blind to those small details, once I have several code blocks.

1 Like

Honestly, not really. If you find that there’s weird behaviour, it is often something like this. In most IDES/Text Editors (of which the CC learning environment is one), if you click on an opening/closing bracket, it will highlight the corresponding closing/opening bracket.

1 Like

Hey guys I think I’m having a similar problem on the previous step for this project. I did find one missing bracket after reading this thread and that solved one part but I’m still getting ‘undefined’ fir user choice ‘paper’. Where ‘userChoice === computerChoice’ no problem

const determineWinner = (userChoice, computerChoice) => { if (userChoice === computerChoice) {
return ‘This game is a tie!’;
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘computer won!’;
}else {
return ‘You won son!’;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’) {
return ‘computer won,!’;
}else{
return ‘You won son!’;
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return ‘computer won!’
}else{
return ‘You won son!’
}}}}}

console.log(determineWinner(‘scissors’, ‘paper’))
console.log(determineWinner(‘scissors’, ‘rock’))
console.log(determineWinner(‘scissors’, ‘scissors’))
console.log(determineWinner(‘paper’, ‘rock’))
console.log(determineWinner(‘paper’, ‘scissors’))
console.log(determineWinner(‘paper’, ‘paper’))

//it prints to the console

You won son!
computer won!
This game is a tie!
undefined
undefined
This game is a tie!

Hi Everyone,
I’m also struggling with this one. I think I’ve got all the code, but I keep getting the response that ‘userChoice’ is undefined. I’ve tried following the walkthrough, but no luck.

Can someone point me in the right direction?

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 ‘The computer won!’;
} else {
return ‘You won!’;
}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘The computer won!’;
} else {
return ‘You won!’;
}
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘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(userChoice, computerChoice));
};
playgame()

Hello @jenzi123 , welcome to the forums! Could you say on which line this error originates, please? Also, could you format your code according to these guidelines?

Hey guys I think I’m having a similar problem on the previous step for this project. I did find one missing bracket after reading this thread and that solved one part but I’m still getting ‘undefined’ fir user choice ‘paper’. Where ‘userChoice === computerChoice’ no problem

const determineWinner = (userChoice, computerChoice) => { if (userChoice === computerChoice) {
return ‘This game is a tie!’;
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘computer won!’;
}else {
return ‘You won son!’;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’) {
return ‘computer won,!’;
}else{
return ‘You won son!’;
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return ‘computer won!’
}else{
return ‘You won son!’
}}}}}

console.log(determineWinner(‘scissors’, ‘paper’))
console.log(determineWinner(‘scissors’, ‘rock’))
console.log(determineWinner(‘scissors’, ‘scissors’))
console.log(determineWinner(‘paper’, ‘rock’))
console.log(determineWinner(‘paper’, ‘scissors’))
console.log(determineWinner(‘paper’, ‘paper’))

//it prints to the console

You won son!
computer won!
This game is a tie!
undefined
undefined
This game is a tie!

Look at this line:

where is the closing brace (}) which matches the opening brace ({)?

Thankyou ‘codeneutrino’ I was managing the closing brackets poorly.

1 Like

When it is determining the winner, it will only output you won or the game is tied, even if the parameter state the computer as the winner.

// the below code allows the user to select a choice of either rock, paper or scissors
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === "rock") {
return "Rock.";
} else if (userInput === "paper") {
return "Paper.";
} else if (userInput === "scissors") {
return "Scissors.";
} else if (userInput === "bomb") {
return "Bomb!";
} else {
return "Ivalid option, try again.";
}
};

console.log(getUserChoice("scissors"));

// the below code is for the computer to select an option
let getComputerChoice = () => {
randomNumber = Math.floor(Math.random() * 3);
// to check if the random function is working just place it in a console.log()
if (randomNumber === 0) {
return "Rock.";
} else if (randomNumber === 1) {
return "Paper.";
} else if (randomNumber === 2) {
return "Scissors.";
}
};

console.log(getComputerChoice());

// the code below compares the choice selected by the user and the computer

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return "The game is tied!";
}

if (userChoice === "rock" && computerChoice === "paper") {
return "Computer won!";
} else {
return "You Won!";
}

if (userChoice === "paper" && computerChoice === "scissors") {
return "Computer won!";
} else {
return "You Won!";
}

if (userChoice === "scissors" && computerChoice === "rock") {
return "Computer won!";
} else {
return "You Won!";
}

if (userChoice === "bomb") {
if (
computerChoice === "rock" ||
computerChoice === "paper" ||
computerChoice === "scissors"
) {
return "You Won!";
}
}
};

console.log(determineWinner("rock", "paper"));

// the code below lets you play the game.

const playGame = () => {
const userChoice = getUserChoice("rock");
const computerChoice = getComputerChoice();
console.log(`You choose \${userChoice}`);
console.log(`Computer choose \${computerChoice}`);

console.log(determineWinner(userChoice, computerChoice));
};

playGame();```