I am facing an issue when attempting to return userInput within a fat-arrow function using the conditional operator. Any suggestions are appreciated.
My code works perfectly fine in ES5;
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' ) {
return userInput;
} else {
console.log('Error!');
}
console.log(getUserChoice('Paper')); // console prints 'paper'
console.log(getUserChoice('fork')); // console prints 'Error!' and `undefined`
However, when I switch to ES6 fat-arrow syntax and the conditional operator, an error occurs. Please note: I want to immediately return the userInput once the first condition of the if..else statement is met.
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors')? return userInput : console.log('Error');
};
console.log(getUserChoice('Paper'));
console.log(getUserChoice('fork'));
The following error is displayed:
(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors')? return userInput : console.log('Error');
^^^^^^
SyntaxError: Unexpected token return