Invalid data in a challenge

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator

In this challenge there is invalid example of conditional operator. There is:

function findGreater(a, b) { return a > b ? "a is greater" : "b is greater"; }

But in the “Get a Hint” section example has brackets in it, so the valid example is:
function findGreater(a, b) { return (a > b ? "a is greater" : "b is greater");.

Parentheses have two syntactic meaning in Javascript: to enclose parameters to a function, and to be a grouping operator (think PEMDAS, order of operations in arithmetic).

So, this is not an error. the return statement doesn’t take parameters, it returns the value of whatever expression is to the right of it:

1 Like

When i typed the answer, cosole didn’t pass my answer, cause there were no parentheses in it, but they were needed. I checked “Get a Hint” section, saw the parentheses in it and wrote them down, so i passed the challenge.

I’m not right. I didn’t pass it.

But now there is another mistake. There is not valid hint :smile:

Well, not passing the test but getting things logically correct can sometimes be a function of a logical error in the testing suite, and there is a place on the github for that. But before I’d launch an issue there, I’d ask to see your code between [SPOILER] code here [/SPOILER] tags.

But to show you that what I am saying is correct:
http://jsfiddle.net/vipatron/5ct0ndbs/2/

1 Like

Look at the example of conditional operator here: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator
and look at the hint here: https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/use-the-conditional-ternary-operator/

In the example they didn’t use parantheses, but in the hint they appear. There is another issue: if copy/paste solution in the hint, code doesn’t work. Check it by yourself.

The reason the solution doesn’t work is that it is erroneous for a different, common reason. The syntax of the return statement isn’t the problem. It’s that they are using the assignment operator, =, instead of the equality operator ==. Fix that, and you’ll see that it passes.

FYI, the assignment operator both assigns the value of the expression on the right to the variable on the left AND returns the value of the expression on the right, so in the return statement, as long as b isn’t 0 (a falsy value), the condition will always be true, so the checkEqual always returns true unless b == 0, which isn’t what we want.

So, congratulations on finding an error! Time to contribute, and gain the honor and esteem of your fellow coders! Open up an issue at the github: https://github.com/freeCodeCamp/freeCodeCamp/issues

1 Like

OK!

Thank you! :slight_smile:

1 Like