Rock Paper Scissors Review

Hello, I’ve just completed the JS Rock Paper Scissors project and could do with some feedback if possible.

Code:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
    return userInput;
  } else {
    console.log('Invalid Selection.')
  }
}

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

function determineWinner(userChoice, computerChoice) {
 if (userChoice === computerChoice) return 'Game was a tie!'
 if (userChoice === 'bomb') return 'Player won!'
 if (userChoice === 'rock') {
   if (computerChoice === 'paper') {
     return 'Computer won!'
   } else {
     return 'Player won!'
   }
 }
 if (userChoice === 'paper') {
   if (computerChoice === 'scissors') {
     return 'Computer won!'
   } else {
     return 'Player won!'
   }
 }
 if (userChoice === 'scissors') {
   if (computerChoice === 'rock') {
     return 'Computer won!'
   } else {
     return 'Player won!'
   }
 }
}

function playGame(userChoice, computerChoice) {
  console.log(`Player chose: ${userChoice}`);
  console.log(`Computer chose: ${computerChoice}`);
  console.log(determineWinner(userChoice, computerChoice));
}

console.log(playGame(getUserChoice('scissors'), getComputerChoice()));

It appears to work though I’m getting an undefined value from somewhere but can’t seem to pinpoint where.

Output:

Player chose: scissors
Computer chose: rock
Computer won!
undefined

playGame function doesn’t return anything, yet when you call the function you log (or should i say: attempt to log) the returned value.

1 Like

Thanks, I changed the function to:

function playGame(userChoice, computerChoice) {
  console.log(`Player chose: ${userChoice}`);
  console.log(`Computer chose: ${computerChoice}`);
  return determineWinner(userChoice, computerChoice);
}
1 Like