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.

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top