Rock, Paper, Scissors Project

Hey guys! I’m having a bit of trouble figuring this out. It’s bugging me. I’ve rewritten the code and still it gives me the same error message. The console is giving me an error message at the line of code where it says: if(userChoice === ‘rock’) … Apparently userChoice is not defined. How do I resolve this issue?

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

const getComputerChoice = () => {
let num = Math.floor(Math.random()*3)
switch (num) {
case 0 :
console.log(‘rock’)
break;
case 1 :
console.log(‘paper’)
break;
case 2 :
console.log(‘scissors’)
break;
}
}

const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return ‘Tie Game’;
}
}

if (userChoice === ‘rock’) { // It’s causing an error here.
if (computerChoice === ‘paper’) {
return ‘Computer Won’;
} else {
return ‘You Won’;
}
}

if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return ‘Computer Won’
} else {
return ‘You Won’
}
}

if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘Computer Won’
} else {
return ‘You Won’
}
}

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

You have a } in the wrong place in the code above.
In the function determineWinner, there’s a } just after the first if-block that doesn’t belong there.
It belongs at the end of the function instead (meaning after the end of the last if-block).

Details

Here’s a formatted version of that function’s code (with indents to keep track of the blocks)
with the locations of the curly-bracket problems pointed out.

Yesss! That was the problem. It worked! Thank you!

1 Like