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

Created on 22 May 2020  ·  16Comments  ·  Source: freeCodeCamp/freeCodeCamp


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:

first timers only help wanted learn

Most helpful comment

"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.

All 16 comments

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.

I do not think this is what the challenge asks for: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

This is the code from the docs:

function example(…) {
    return condition1 ? value1
         : condition2 ? value2
         : condition3 ? value3
         : value4;
}

What you have constructed is:

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

I agree, you have _accomplished_ the task. However, the expected implemented logic is of the form:

function findGreaterOrEqual(a, b) {
  if (a === b) {
    return "a and b are equal";
  }
  else if (a > b) {
    return "a is greater";
  }
  else {
    return "b is greater";
  }
}

So, perhaps we should change the challenge instructions to be more specific as to the format...

I do not think this is what the challenge asks for: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

This is the code from the docs:

function example(…) {
    return condition1 ? value1
         : condition2 ? value2
         : condition3 ? value3
         : value4;
}

What you have constructed is:

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

I agree, you have _accomplished_ the task. However, the expected implemented logic is of the form:

function findGreaterOrEqual(a, b) {
  if (a === b) {
    return "a and b are equal";
  }
  else if (a > b) {
    return "a is greater";
  }
  else {
    return "b is greater";
  }
}

So, perhaps we should change the challenge instructions to be more specific as to the format...

Sir, please note: this repository is of Free Code Camp. Thank You for your reply.

@FelixBoscoJ , haha. Yes, I am well aware. I linked to the Mozilla documentation to retrieve an example of Multiple Conditional Ternary Operators.

My point is still entirely valid.

@FelixBoscoJ , haha. Yes, I am well aware. I linked to the Mozilla documentation to retrieve an example of Multiple Conditional Ternary Operators.

My point is still entirely valid.

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

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

````
Please try the above code in this challenge. Please check what is required to pass the challenge.

@FelixBoscoJ , I have, and I understand exactly why this issue was created. I am pointing out that the format of your solution is not what is expected, which is clearly seen in the example given in the lesson:

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

I agree, you have accomplished the task. However, the expected implemented logic is of the form:

function findGreaterOrEqual(a, b) {
  if (a === b) {
    return "a and b are equal";
  }
  else if (a > b) {
    return "a is greater";
  }
  else {
    return "b is greater";
  }
}

Your solution does not contain else if logic.

@FelixBoscoJ , I have, and I understand exactly why this issue was created. I am pointing out that the format of your solution is not what is expected, which is clearly seen in the example given in the lesson:

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

I agree, you have accomplished the task. However, the expected implemented logic is of the form:

function findGreaterOrEqual(a, b) {
  if (a === b) {
    return "a and b are equal";
  }
  else if (a > b) {
    return "a is greater";
  }
  else {
    return "b is greater";
  }
}

Your solution does not contain else if logic.

Sir the condition to win the challenge is:

Use multiple conditional operators in the checkSign function to check if a number is positive, negative or zero. The function should return "positive", "negative" or "zero".

We should Use Multiple Conditional (Ternary) Operators.

Perhaps the error message should be "... multiple conditional operators in the recommended format."

And perhaps adding a note along the lines of "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."

"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.

Closed? Was an update to the description merged?

@Sky020 It is not possible to implement if/else if/else logic using ternary Operator. But the task can be accomplished.

I agree, you have accomplished the task. However, the expected implemented logic is of the form:

 function findGreaterOrEqual(a, b) {
   if (a === b) {
     return "a and b are equal";
   }
   else if (a > b) {
     return "a is greater";
   }
   else {
     return "b is greater";
   }
 }

No, the expected implemented logic is of the form:
```js
function findGreaterOrEqual(a, b) {

if (a === b) {
return "a and b are equal";
}
else {
if (num>0) {
return "positive";
} else {
return "negative";
}
}
}

```

@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.

Nevermind.

It looks to me like adding clarifying comments is the best solution. So, let's leave this open until a PR gets merged.

I'd like to jump on this, if possible. I'm a first-timer but really excited to learn!

Go for it, @jasnkwcz. This should be a good issue for a first-timer.

I recommend taking a look at the contribution guide, and if you get stuck ask away in the contributor's chat room

Was this page helpful?
0 / 5 - 0 ratings