Why doesn't the ternary operator work in this challenge: "Object Oriented Programming: Understand the Constructor Property"?

I’m talking about this challenge:

Why doesn’t this solution:

function joinDogFraternity(candidate) {
  (candidate.constructor === Dog) ? true : false;
}

work in this challenge and in Javascript even though that ternary expression inside is just the same as:

  if (candidate.constructor === Dog) {
    return true;
  } else {
    return false;
  }

?

well, you don’t have a return statement there… the function just returns undefined

Ok so I looked up a bit and it does need an explicit return keyword before the ternary expression, but I didn’t think it needed it since I had known the ternary expression format implies the values between the colon will return either of them (depending if true of false).

Oh, and geez, thanks for those three dots that exuded a bit of passive-aggressiveness as if it’s just a silly mistake that I just forgot the return keyword even though my question is a valid question for a beginner.

I really do not think that the three dots are intended to communicate some sort of passive aggressive judgement on your skills.

the ternary operator returns a value, but using a ternary operator doesn’t imply that the value is also returned from the function, you need to explicity state what value is returned from the function

If you want to make use of the implicit return of a ternary in a function, you’d have to write it as arrow function:

const joinDogFraternity = candidate => candidate.constructor === Dog ? true : false;

As already stated above, functions declared with the function keyword always need an explicit return keyword.

2 Likes

Thanks for the alternate solution! I think this and the solution using return statement with a ternary operator should also be in the official ‘Get a Hint’ solutions post and include an explanation regarding needing an explicit return keyword.