Rock, Paper, or Scissors

Hi guys! I have noticed that there are other topics about this but I got stuck and I really can’t understand why,

this is the link of the exercise

this is what I wrote

const getUserChoice = userChoice => {

userChoice = userChoice.toLowerCase();

if (userChoice == 'paper' || userChoice == 'rock' || userChoice == 'scissors') {

  return userChoice;

   } else {

  return 'Error! please insert paper, rock or scissors!';

}

}

const getComputerChoice = computerChoice => {

computerChoice = Math.floor(Math.random() * 3);

switch(computerChoice){

case 0:

return 'paper';

case 1:

return 'rock';

case 2:

return 'scissors';

}

}

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!';

}

}

}

console.log(getUserChoice(‘Paper’))

console.log(getComputerChoice());

console.log(determineWinner());

with the 3 console.log I have noticed that the 2 single functions getUserChoice and getComputerChoice work properly, in fact those log the right informations,
the issue is that in the function determinateWinner the values becose undeterminate so the result of this function is always a tie because undeterminate === undeterminate so it returns me a tie,
could you help me to understand where is the error please?

Your function determineWinner() takes two parameters as input, userChoice and computerChoice, however your function call at the end isn’t entering any arguments. Therefore it’s carrying through undefined for both userChoice and computerChoice.

I also tried calling the function with parameters inside so:
console.log(determineWinner(getUserChoice, getComputerChoice)); and the result is undeterminate
console.log(determineWinner(userChoice, computerChoice)); and the result is an error,

I should I do?

getUserChoice and getComputerChoice are both functions which accept one argument each. Unlike many languages, any unassigned non-optional parameters in JS get automatically assigned the type undefined, which in itself doesn’t throw an error. The problem is, the undefined type can’t have much done on it, so acting upon undefined, like undefined.toLowerCase() throws an error.

1 Like