Use Multiple Conditional (Ternary) Operators

Use Multiple Conditional (Ternary) Operators
0.0 0

#1

Tell us what’s happening:
It says to Use multiple conditional operators in the checkSign function to check if a number is positive, negative or zero. but I’m not very good with them I’ve been stuck on this for nearly an hour trying stuff so I’d apreciate some help.

Your code so far


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

checkSign(-12);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/use-multiple-conditional-ternary-operators


#2

First your code checks if num === num. That is always going to be true (unless num is NaN), so your function is going to return “positive” for every test.

First see if you can create the same function using if/else if/else statements. Once you get that working, then try using ternary operators.

Currently, your algorithm seems to be incorrect. You need to ask these two basic questions:

  1. How do you check if a number is positive?
  2. How to you check if a number is negative?

Once you have code that checks for these conditions, then any other number passed into the function must be zero.


#3

Ok I’ll start by looking up how to check if a number is positive thanks for the help.


#4

return (num > 0) ? “positive” : (num < 0) ? “negative” : “zero”;

This is the solution for this exercise.


#5

its not passing the exercise


#6

function checkSign(num) {
return (num === 0)?“zero”:(num < 0)?“negative”:“positive”;
}

checkSign(10);
This is the solution.