Javascript Rock, Paper Scissors Project Determine Winner Function Not Working

Code for (almost) the entire project below.

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.

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
//Begin user input
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’) {
return userInput
}
else {
return(‘Invalid selection please try again’);
};
};

//begin computer choice
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 = (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!’;
}
};

//begin game

const playGame = () => {
const userChoice = getUserChoice(‘scissors’);
const computerChoice = getComputerChoice();
console.log('you threw ’ + userChoice);
console.log('computer threw ’ + computerChoice);

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

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!’.

(1) if (userInput === 'rock') {
(2) if (computerChoice === 'paper') {
return 'Computer Wins!
}
}
(3) else {
return 'User Wins!'; {
}

There is three mistakes you made:

  1. 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!

Hi there guys, I keep getting
/home/ccuser/workspace/sleep-debt-calculator/sleepDebtCalculator.js:39
});
^
SyntaxError: Unexpected token )

And I have absolutely no clue where to look since I have 37 lines of code ¯_(ツ)_/¯ please help…

const getSleepHours = (day) => {
if(day === ‘monday’) {
return 7
} else if(day === ‘tuesday’) {
return 7.5
} else if(day === ‘wednesday’) {
return 7
} else if(day === ‘thursday’) {
return 6
} else if(day === ‘friday’) {
return 9
} else if(day === ‘saturday’) {
return 8.5
} else if(day === ‘sunday’) {
return 6
} else {
return ‘Please enter a valid day of the week’
}
}
const getActualSleepHours = () =>
getSleepHours(‘monday’) + getSleepHours(‘tuesday’) + getSleepHours(‘wednesday’) + getSleepHours(‘thursday’) + getSleepHours(‘friday’) + getSleepHours(‘saturday’) + getSleepHours(‘sunday’)

const getIdealSleepHours = () => {
let idealHours = 8
return idealHours * 7
}
const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours()
const idealSleepHours = getIdealSleepHours()
if(actualSleepHours === idealSleepHours) {
console.log(‘You got the perfect amount of sleep!’)
} else if(actualSleepHours > idealSleepHours) {
console.log(‘You got ’ + (actualSleepHours - idealSleepHours) + ’ Hour(s) more sleep than you needed!’)
} else {
console.log(‘You got ’ + (idealSleepHours - actualSleepHours) + ’ hour(s) less sleep than you needed!’)
}
calculatesSleepDebt()

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()

1 Like