Ad
  • Custom User Avatar

    Not in this situation.

  • Custom User Avatar

    The problem with Math.sign is that it only works on numbers, while we might be comparing arbitrary comparable objects which doesn't admit a numeric rank.

  • Default User Avatar

    It is performing the sign function, which Javascript does not natively support (yet*, although it is being added: Math.sign(c) would give the same result in a Javascript environment which supports the new function). You will also get a number's sign if you divide it by its absolute value; since Javascript does support the abs function, you could write c / Math.abs(c) to get the same result.

    The ternary statement there is equivalent to writing,

    if (c < 0) {
      return -1;
    } else if (c > 0) {
      return 1;
    } else {
      return 0;
    }
    

    Since it's inline, you can write a statement such as return a ? b : c or result = a ? b : c and it's much simpler and more concise than writing the equivalent code.

    *Actually since CodeWars supports ES6 now, Math.sign will work in the JS framework. For use in web programming, though, browser support of the feature is still limited.

  • Custom User Avatar