Tell us what’s happening:
Bug - Test for below code fails with error: checkSign should use multiple conditional operators.
Your code so far
function checkSign(num) {
return (num!==0) ? (num>0) ? "positive"
: "negative"
: "zero";
}
console.log(checkSign(10));
console.log(checkSign(-1));
console.log(checkSign(0));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36 OPR/67.0.3575.97
.
Challenge: Use Multiple Conditional (Ternary) Operators
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
It doesn’t like this line.
return (conditional operator) ? result
: conditional operator ? result
: result
1 Like
Too funny. FCC enforces ‘strict mode’ to get users to write better code but teaches multiple ternary operators.
1 Like
I think the challenge fails to check for Ternary operators in statement-if-true part of Ternary operator.
Multiple ternaries can be useful in specific circumstances and they can be clearer than multiple if statements in some cases.
The challenge gave you an expected format for your multiple ternary. Does the test pass if you change your code to match the described best practice format?
1 Like
const checkSign = (num) => ['zero', 'negative', 'positive'][(num**2 > 0) + (num > 0)];
This is a lot cleaner. I never use multiple ternary. Just bad programming.
1 Like
Yes, Sir. The test pass if I change my code to match the described best practice format.
function checkSign(num) {
return (num===0) ? "zero"
:(num>0) ? "positive"
: "negative" ;
}
console.log(checkSign(10));
1 Like
That’s terrible for clarity (in my opinion), but it gets props for looking clever.
1 Like
Huzzah. I’m glad to have helped.
1 Like
I think there must also be clarity in the error message displayed by the challenge. Sir.
Instead of Error: “checkSign should use multiple conditional operators.”
Using multiple ternary operators in statement-if-true part of ternary operator is not best practice.
That’s fair. If you like, you can post this as an issue on the Free Code Camp GitHub, or I can. Describing issues is a good skill to practice but I don’t mind writing it up either.
1 Like
I think the work of the developer is to develop. If I post this issue on Free Code Camp GitHub, I get a feeling of developing something Sir.
1 Like
Huzzah. It’s good to be part of the community.
1 Like