-2

So I've been running the below JavaScript code

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || 'paper' || 'scissors') {
    return userInput
  } else {
    console.log('Error: please give relevant input \'rock\' \'paper\' or \'scissors\'.')
  }
}

getUserChoice('spock')
console.log(getUserChoice('paper'))

The code does correctly print out paper but it doesn't print the console.log error from the else conditional on the first call of the function with 'spock'

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313

1 Answers1

0

Quite a common mistake, you cannot nest that way:

userInput === 'rock' || 'paper' || 'scissors'

you need to :

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

or if you want with a little RegExp.prototype.test() for case insensitive i:

if ( /^(rock|paper|scissors)$/i.test(userInput) ) {

or if you like Arrays than using Array.prototype.includes():

if ( ["rock", "paper", "scissors"].includes(userInput) ) {
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313