When I run the game it doesn’t determine the correct winner if the userInput is ‘paper’ or ‘scissors’ (it will always say the user wins), it only works correctly with ‘rock’ and if there is a tie. Not sure what I did wrong here and any input would be greatly apprecaited.
Hi!
The if else order is broken.
Try to “read” your code and the error will be clear:
If the user chooses ‘rock’, it starts the first (1) part of the code and then checks (2) if the computer choice is ‘paper’. If it is, it outputs the answer 'Computer Wins! What happens if the computer selection is not ‘paper’? Nothing, because there is no other choice inside.
If the user selects something other than ‘rock’, it goes straight to else (3) and gives the answer ‘User Wins!’.
Your quotation mark is wrong.
2.You passes wrong parameter in determineWinner function
3.You use else statement in wrong place in determineWinner function.
As a newbie maybe there is something I missed from the previous lessons. I thought that if the if/else statement condition wasn’t fulfilled it would continue down the line until it found the conditions that are met? Would this be better written as an if/else if/else statement then? The project itself said that we didn’t need to program out every possible combination, and the code itself is from the video tutorial… where I learned about the nested if statements that are used here.
Oddly, putting in ‘rock’ is the only time the code works correctly, any other selection (‘paper’ or ‘scissors’) I get that the user wins and it will populate correctly if its a tie between the user and computer.
You have some } in the wrong place, so the if-statement blocks are not nested correctly
(meaning that the code you posted does not have some of the if and else stuff inside other if and else stuff the way it should).
Your code, with indentations to match the { and }, is below:
const determineWinner = (userInput, computerChoice) => {
if (userInput === computerChoice){
return 'The game is a tie!';
}
if (userInput === 'rock') {
if (computerChoice === 'paper') {
return 'Computer Wins!';
}
}
else {
return 'User Wins!';
}
if (userInput === 'paper') {
if (computerChoice === 'scissors') {
return 'Computer Wins!';
}
}
else {
return 'User Wins!';
}
if (userInput === 'scissors') {
if (computerChoice === 'rock') {
return 'Computer Wins!';
}
}
else {
return 'User Wins!';
}
};
but those else statements should be nested (meaning inside the if-statement blocks),
as below:
const determineWinner = (userInput, computerChoice) => {
if (userInput === computerChoice){
return 'The game is a tie!';
}
if (userInput === 'rock') {
if (computerChoice === 'paper') {
return 'Computer Wins!';
}
else {
return 'User Wins!';
}
}
if (userInput === 'paper') {
if (computerChoice === 'scissors') {
return 'Computer Wins!';
}
else {
return 'User Wins!';
}
}
if (userInput === 'scissors') {
if (computerChoice === 'rock') {
return 'Computer Wins!';
}
else {
return 'User Wins!';
}
}
};
I also recommend changing some of the if to else if .
Thank you! I kept checking the brackets to see if that was an issue but I couldn’t figure it out. This was my first time using a nested “if/else” statement. I was also wondering if an “if/else if/else” would have worked better here. Thank you so much for your help and advice!
Based on the code you posted, in the second last line, you are missing a closing curly brace. That missing curly brace is for closing the body of the calculateSleepDebt function.
Also, on the last line, you have calculatesSleepDebt() whereas the function is named calculateSleepDebt()