freeCodeCamp: Bug in Challenge: Use Multiple Conditional (Ternary) Operators.

Describe your problem and how to reproduce it: Bug - Test for below code fails with error: checkSign should use multiple conditional operators.

function checkSign(num) {
  return (num!==0) ?(num>0) ? "positive" 
    : "negative" 
    : "zero";
}


console.log(checkSign(10));
console.log(checkSign(-10));
console.log(checkSign(0));

Screenshot -1: image

Screenshot - 2: image

How to reproduce It. Instead of Error: “checkSign should use multiple conditional operators.” Error should be: Using multiple ternary operators in statement-if-true part of ternary operator is not best practice.

(Increasing clarity of error message)

Add a Link to the page with the problem: Link to Challenge; Link to my post reporting bug on forum:

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (11 by maintainers)

Most upvoted comments

“It’s is a best practice to format the multiple conditional operators so that each condition is on a separate line, as shown in the example, for readability.”

I would be for adding a note similar.

I agree that some tweaking makes sense. I’d would help if we can capture the fact that the test fails if a) you aren’t using a multiple ternary or b) the ternary isn’t in the expected format.

@FelixBoscoJ

Expected solution (one of 6, very similar to your final answer):

function checkSign(num) {
  return (num > 0) ? "positive" 
    : (num < 0) ? "negative"
    : "zero";
}

If-else version

function checkSign(num) {
  if (num > 0) {
    return "positive";
 } else if (num < 0) {
    return "negative";
 } else {
    return "zero";
 } 
}

A ternary, or multiple ternary, always can be written with if-else. The challenge and the description is accurate. We just want to add a clarifying comment or two to emphasize the best practice formatting for readability.