0

I wrote the function to check if a given number is positive, negative, or zero. Is there a shorthanded way?

Is it possible to test the condition with map (using object to index the conditions)? i was inspired by second solution in this question but not sure how it applies here.

const numCheck = (num) => {
  if (num == 0) {
    return "zero";
  } else {
    return num > 0 ? "positive" : "negative";
  }
};

const num1 = 3;
console.log(numCheck(num1));
jazzfaz
  • 143
  • 1
  • 10
  • 1
    There are other options, but what you have is the clearest IMO. (except, use `===`, not `==`) – CertainPerformance Jan 09 '22 at 05:10
  • would map be even more clear?? im inspired by the second solution here but not sure how it applies to my code. https://stackoverflow.com/a/18269354/13782899 – jazzfaz Jan 09 '22 at 05:11
  • `const numCheck = (num) => num === 0 ? 'zero' : num > 0 ? 'positive' : num < 0 ? 'negative' : 'undefined';` – Feelsbadman Jan 09 '22 at 05:14
  • @jazzfaz with the map method you mentioned you will need infinite amount of keys? the answers here give solution using ternary operator. thats the shortest i guess – cmgchess Jan 09 '22 at 05:21
  • @cmgchess i don't have infinite keys, but i couldn't figure out with map method how to use condition as a key, since the example was string match – jazzfaz Jan 09 '22 at 05:25
  • 1
    @jazzfaz The mapping approach only works when you have exact values, so it could replicate `num == 0` but not `num > 0` (or `num < 0` for that matter) – Lennholm Jan 09 '22 at 05:25
  • 1
    aha i got it! so map only works for fixed key match..not conditions – jazzfaz Jan 09 '22 at 05:26
  • `[100, 1, 0, -1, -100].map(x => !x ? "zero" : x > 0 ? "positive" : "negative");` – epascarello Jan 09 '22 at 05:27

4 Answers4

2

In the solution below, the numCheck() method is tested by using the Array.prototype.map() method, considering three possible situations.

const numCheck = (num) => {
  return (num == 0) ? "zero" : ((num > 0) ? "positive" : "negative");
};

const numbers = [1, -1, 0];

console.log(numbers.map(number => numCheck(number)));
Sercan
  • 4,739
  • 3
  • 17
  • 36
1

you can do that, simply use Math.sign() :

const numCheck = n => ['negative','zero','positive'][1+Math.sign(n)]
 
console.log( numCheck( 4 ) ) 
console.log( numCheck( 0 ) ) 
console.log( numCheck( -3 ) ) 
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
  • haha this is not very readable but genius to think of! – jazzfaz Jan 09 '22 at 07:14
  • @jazzfaz Thanks :) ,hmm, i'm willing to admit it's unusual, but for me it's still readable, let's say it's just [thinking outside the box](https://en.wikipedia.org/wiki/Thinking_outside_the_box#Nine_dots_puzzle) ? – Mister Jojo Jan 09 '22 at 16:15
0

A shorter solution:

var numCheck = (num) => num == 0 ? "zero" : num > 0 ? "positive" : "negative";
const num1 = 3;
console.log(numCheck(num1));
Faly
  • 13,291
  • 2
  • 19
  • 37
0

Inspired by Mister Jojo answer but without Math.sign

const sign = x => ['negative','zero','positive'][1 + (x > 0) - (x < 0)]
 
console.log(sign(10))  // positive
console.log(sign(0))   // zero
console.log(sign(-20)) // negative
Tolotra Raharison
  • 3,034
  • 1
  • 10
  • 15