Domanda

    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.

È stato utile?

Soluzione

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top