Positive Number to Negative Number in JavaScript?

后端 未结 13 1698
误落风尘
误落风尘 2020-12-13 05:16

Basically, the reverse of abs. If I have:

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


        
13条回答
  •  既然无缘
    2020-12-13 05:46

    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

提交回复
热议问题