Rock paper scissors project (JavaScript) SyntaxError

Hi all!
I am working through the Rock paper scissors project on JavaScript tutorial.

I tried to use ternary operator instead of if / else statement, however received SyntaxError. Apparently, there is something wrong with using ‘return’ here, but I don’t quite understand why. Replacing “return userInput” with something else works fine, as well as writing the if function the ordinary way with “return userInput” in code block.

The code:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  userInput === 'rock' || 'paper' || 'scissors' ? return userInput : console.log(`${userInput} is an ivalid entry. Only values 'rock', 'paper' or 'scissors' allowed!`)
}
getUserChoice('scissors');

The error message:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:3
  userInput === 'rock' || 'paper' || 'scissors' ? return userInput : console.log(`${userInput} is an ivalid entry. Only values 'rock', 'paper' or 'scissors' allowed!`)
                                                  ^^^^^^
SyntaxError: Unexpected token return

Please explain :upside_down_face: I did not find the exact same problem while searching.

Hi,

In English, saying if a then b or c is ambiguous if read literally. The compilers will always read this from left to right.

So if today rains I will wear an umbrella or I will not leave the house gets interpreted as:
if today rains I will wear an umbrella
OR
I will not leave the house.

So if I don’t leave the house the whole statement will evaluate to true (regardless of whether I don’t bring an umbrella if it rains, or if it doesn’t rain).

You have to explicitly state your conditions: if A then B OR A then C.

Of course, you have an equals condition (if A==B), not an implication condition (if A then B), but the idea holds.

@toastedpitabread, than you for answering. However, I am not quite sure I got your idea. If you meant that each of the OR parts should contain === (as below), then this does not solve the issue, the same error message is there.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' ? return userInput : console.log(`${userInput} is an ivalid entry. Only values 'rock', 'paper' or 'scissors' allowed!`)
}
getUserChoice('scissors');

whereas this code for example (without ‘return’) runs fine:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  userInput === 'rock' || 'paper' || 'scissors' ? console.log('This code will run!') : console.log(`${userInput} is an ivalid entry. Only values 'rock', 'paper' or 'scissors' allowed!`)
}
getUserChoice('scissors'); // This code will run!  is printed

Well, let me revise @ruby9053139199:
Try running

5 == 2 || 3 ? '5 does equal it' : 'something else'; 

This returns 5 does equal it, is that desired behavior if we want to find out if 5 == 2 or 5 == 3?

Then back to your original question. The return is implicit. No need to state it. See documentation: Conditional (ternary) operator - JavaScript | MDN

hmm… still don’t get what way should it be written correctly or the ternary is simply not applicable here. Might it be that I am providing statement where expression is expected?

Note if I change your code a little:

const changedFn = userInput => {
  userInput = userInput.toLowerCase();
  'paper' || 'scissors' ? console.log('This code will run!') : console.log(`${userInput} is an ivalid entry. Only values 'rock', 'paper' or 'scissors' allowed!`)
}
changedFn("bubbles")

it returns "This code will run!"

If your doubt is about the ternary, if I write

function f(input){
  if ('paper' || 'scissors'){
    return "This runs!";
  } else {
    return "it didn't run";
  }
}
console.log(f("bubbleeeees"));

This will also print "This runs!" to the console.

In conditionals: any non-zero, non-null, non-false object will evaluate to true. (It is basically a boolean test of it’s existence). So if ("x") will trigger unconditionally. So will if ("false") (because it’s a string, and not the bool false).

The implication of this is that any object that doesn’t evaluate to false by itself immediate will validate an OR proposition.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.