Positive Number to Negative Number in JavaScript?

后端 未结 13 1636
误落风尘
误落风尘 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 06:06

    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.

提交回复
热议问题