What's wrong with this ternary operator?


#1

Ternary Operator not working as expected.

The problem

Hi. This codeblock is actually from This Lesson , but I couldn't find where to post this.

Well that, and it's more a question on the control flow of this portion of code, rather than how to solve the exercise. The problem is, this code just wont work (as i expect the ternary operator should), although i'm pretty confident it's correct. (Of course i could be wrong.

Here’s the code:

function meetsStaffRequirements(availableStaff, requiredStaff){
  return (availableStaff.length >= requiredStaff) ?
  	true: 
  	false;
};

What i've tried:

  • Changed it to a normal if statement, this works
  • Switched it back to a ternary operator: doesn't work
  • Checked MDN Docs to be sure i'm using correct syntax.

Expected and Outcome

Expected: Function passes true or false, depending on whether the comparison is true or false.

Actual Outcome: Unexpected token ``` return ```

Solution

How: Move return to the beginning of the comparison. Why: Return is it's own statement, code that performs an action. We can't put statements inside an expression . ``` (availableStaff.length >= requiredStaff) ? return true: return false; ``` is trying to return a statement inside an expression, and won't work.

#2

What’s expected and what’s the outcome? What’s the version using an if-statement?

avoid that word when describing something.

Also, you’re better off with:

function meetsStaffRequirements(availableStaff, requiredStaff){
  return availableStaff.length >= requiredStaff;
};

Since your expression already produces the value that you mean to return.
(But that doesn’t answer your question)

Oh and if you READ that expression, it doesn’t make sense. So perhaps you used a different expression in your if-statement.


#3

Avoid that word when describing something.

Yep, changed that.

It makes sense in the context: availableStaff is an array, required staff is a number to compare it to.

Got to the solution eventually, both with your input and chipping away at it. Point to note: return has to be at the beginning of the statement.
Thanks :slight_smile:


#4

Then I argue the names are bad. But alright. Value-wise making sense.

What do you mean by putting return at the beginning of the statement? Now I’m curious


#5

Oh. You probably had something like a parenthesis left open earlier on in the code? Not in the code in your post though! That’s valid syntax


#6

Heh, i agree. Unfortunately that’s the names the lesson assigned.

Originally, I was trying to make this work:
(availableStaff.length >= requiredStaff) ? return true: return false;
Which, in my head makes sense: check that number against the other number, if it’s true, return true, if it’s false, return false.
Makes logical sense to me.

Changing the statement to:
return (availableStaff.length >= requiredStaff) ? true: false; is what made it work.
So if i speak that out, it still makes sense but isn’t inherantly logical to me:
Return the result of that comparison, then tell me whether it’s true or false.

Not as flowing as the other version was to me, but it’s the solution to it i guess lol!


#7

Right. An expression may be a statement.
But return is a statement all by itself and you can’t have statements inside an expression

An if-statement on the other hand can have zero or more statements in its body


#8

Nah, definitely wasn’t anything out of place otherwise. No idea what was throwing that error.

And I know a lot of people would be looking at this and thinking why not just go with the straight forward if statement?
Which I get. But, to me, ternary operators just seem a lot more concise. I’m happy to use if/else if/else when i really need to , but understanding my other options and how to use them properly is my thing.


#9

Definitely not in this case, you should go for:

return availableStaff.length >= requiredStaff;


#10

Right. Just feels like the better use of code here.
Also, in your other post

^ this is perfect. Learned a lot from that post alone, so thanks a lot. I’ve got a feeling that understanding is going to save me a lot of heart/headache later.


#12

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