Positive Number to Negative Number in JavaScript?

JavascriptMath

Javascript Problem Overview


Basically, the reverse of abs. If I have:

if ($this.find('.pdxslide-activeSlide').index() < slideNum - 1) {
  slideNum = -slideNum
}
console.log(slideNum)

No matter what console always returns a positive number. How do I fix this?

If I do:

if ($this.find('.pdxslide-activeSlide').index() < slideNum - 1) {
  _selector.animate({
    left: (-slideNum * sizes.images.width) + 'px'
  }, 750, 'InOutPDX')
} else {
  _selector.animate({
    left: (slideNum * sizes.images.width) + 'px'
  }, 750, 'InOutPDX')
}

it works tho, but it's not "DRY" and just stupid to have an entire block of code JUST for a -.

Javascript Solutions


Solution 1 - Javascript

Math.abs(num) => Always positive
-Math.abs(num) => Always negative

You do realize however, that for your code

if($this.find('.pdxslide-activeSlide').index() < slideNum-1){ slideNum = -slideNum }
console.log(slideNum)

If the index found is 3 and slideNum is 3,
then 3 < 3-1 => false
so slideNum remains positive??

It looks more like a logic error to me.

Solution 2 - Javascript

The reverse of abs is Math.abs(num) * -1.

Solution 3 - Javascript

The basic formula to reverse positive to negative or negative to positive:

i - (i * 2)

Solution 4 - Javascript

To get a negative version of a number in JavaScript you can always use the ~ bitwise operator.

For example, if you have a = 1000 and you need to convert it to a negative, you could do the following:

a = ~a + 1;

Which would result in a being -1000.

Solution 5 - Javascript

var x = 100;
var negX = ( -x ); // => -100

Solution 6 - Javascript

Javascript has a dedicated operator for this: unary negation.

TL;DR: It's the minus sign!

To negate a number, simply prefix it with - in the most intuitive possible way. No need to write a function, use Math.abs() multiply by -1 or use the bitwise operator.

Unary negation works on number literals:

let a = 10;  // a is `10`
let b = -10; // b is `-10`

It works with variables too:

let x = 50;
x = -x;      // x is now `-50`

let y = -6;
y = -y;      // y is now `6`

You can even use it multiple times if you use the grouping operator (a.k.a. parentheses:

l = 10;       // l is `10`
m = -10;      // m is `-10`
n = -(10);    // n is `-10`
o = -(-(10)); // o is `10`
p = -(-10);   // p is `10` (double negative makes a positive)

All of the above works with a variable as well.

Solution 7 - Javascript

Are you sure that control is going into the body of the if? As in does the condition in the if ever hold true? Because if it doesn't, the body of the if will never get executed and slideNum will remain positive. I'm going to hazard a guess that this is probably what you're seeing.

If I try the following in Firebug, it seems to work:

>>> i = 5; console.log(i); i = -i; console.log(i);
5
-5

slideNum *= -1 should also work. As should Math.abs(slideNum) * -1.

Solution 8 - Javascript

num * -1

This would do it for you.

Solution 9 - Javascript

If you don't feel like using Math.Abs * -1 you can you this simple if statement :P

if (x > 0) {
    x = -x;
}

Of course you could make this a function like this

function makeNegative(number) {
    if (number > 0) {
        number = -number;
    }
}

makeNegative(-3) => -3 makeNegative(5) => -5

Hope this helps! Math.abs will likely work for you but if it doesn't this little

Solution 10 - Javascript

var i = 10;
i = i / -1;

Result: -10

var i = -10;
i = i / -1;

Result: 10

If you divide by negative 1, it will always flip your number either way.

Solution 11 - Javascript

Use 0 - x

x being the number you want to invert

Solution 12 - Javascript

It will convert negative array to positive or vice versa

function negateOrPositive(arr) {
    arr.map(res => -res)
};

Solution 13 - Javascript

In vanilla javascript

if(number > 0)
  return -1*number;

Where number above is the positive number you intend to convert

This code will convert just positive numbers to negative numbers simple by multiplying by -1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOscar GodsonView Question on Stackoverflow
Solution 1 - JavascriptRichardTheKiwiView Answer on Stackoverflow
Solution 2 - JavascriptJakub HamplView Answer on Stackoverflow
Solution 3 - JavascriptBlakeView Answer on Stackoverflow
Solution 4 - JavascriptBenjamin WilliamsView Answer on Stackoverflow
Solution 5 - Javascripttpayne84View Answer on Stackoverflow
Solution 6 - JavascriptbrentonstrineView Answer on Stackoverflow
Solution 7 - JavascriptVivin PaliathView Answer on Stackoverflow
Solution 8 - JavascriptVibhor DubeView Answer on Stackoverflow
Solution 9 - JavascriptAsh PettitView Answer on Stackoverflow
Solution 10 - JavascriptJeff PriceView Answer on Stackoverflow
Solution 11 - Javascriptmilesman34View Answer on Stackoverflow
Solution 12 - JavascriptFlash NoobView Answer on Stackoverflow
Solution 13 - JavascriptperfoView Answer on Stackoverflow