Rock, Paper, Scissors

Hello,

Please could you help! I have it all working with no errors HOWEVER… It always comes out as a draw.

Any suggestions gratefully received!


const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
   if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
     return userInput;
 } else {
   console.log('Please check your spelling.');
   }
}

// Computer choice Maker

function getComputerChoice () {
  let randomNumber = Math.floor(Math.random () * 3);
  switch (randomNumber) {
    case 0:
    return 'rock';
    case 1:
    return 'paper';
    case 2:
    return 'scissors';  
  }
}

function determineWinner (userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'That game was a draw';
  } 
  if (userChoice === 'rock') {
      if (computerChoice === 'paper') {
        return 'Computer Wins'
    } else {
      return 'You won!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'Computer Wins'
    } else {
      return 'You won!';
    }
  } 
if (userChoice === 'scissors') {
  if (computerChoice === 'rock') {
    return 'Computer Wins';
  } 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());
playGame()

Hello,

Two things I notice, your playGame function is not calling the determineWinner function with the proper input parameters. And since your determineWinner function returns strings also a console.log is needed.

I would expect the following line to be included in the function:

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

The reason you are getting draw printed all the time is because of the line of code just above the playGame() call at the end of your script.

console.log(determineWinner());

Since this line has no input yet expects two (userChoice and computerChoice it will pass the first if statement

if (userChoice === computerChoice) { // no input so it compares two values of undefined
    return 'That game was a draw';
  } 

Since undefined equals undefined.

Happy coding!

2 Likes

Thank you very much for your response. Really appreciate it, everything you have said makes sense now!

2 Likes