Question

    function absolute(number)
    {
    if (number < 0)
        return -number;
    else
        return number;
    }

   console.log(absolute(-144));

prints on console: 144

If(number < 0) then it returns -number. So therefore -144 is less then 0 so it should return -144 , not 144 correct? if its not less then number , then it should be positive number.

This is what i am understanding but its printing 144 on console.

Était-ce utile?

La solution

The absolute() function returns the absolute value of the number.

-144 => 144
0 => 0
42 => 42

You're overthinking it ;)

Easy steps:

  1. Number is negative ( < 0)
  2. We want the absolute value, same value without the sign
  3. So we remove the minus by applying another minus to it
  4. Get the absolute value and rule the world
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top