문제

    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