Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery/Javascript : Negative of a Variable

What's the best way to check for the negative of a variable?

Here are my variables:

var frameWidth = 400;
var imageWidth = parseInt($('#' + divId).find('#inner-image').css('width'), 10);
var imageMargin = parseInt($('#' + divId).find('#inner-image').css('margin-left'), 10);
var numberOfFrames = imageWidth/frameWidth;

I want to perform a check kind of like this:

if (imageMargin == -numberOfFrames*frameWidth-400 )

But I don't know how.

In other words, if numberOfFrames*frameWidth-400 equals 800, I need it to return -800.

Thanks again for any direction you can provide.

like image 928
Rrryyyaaannn Avatar asked Dec 13 '22 15:12

Rrryyyaaannn


2 Answers

There should be no problems if you put parenthesis around the value you want to negate:

if (imageMargin == -(numberOfFrames*frameWidth-400) )
   ...
like image 84
sth Avatar answered Dec 30 '22 16:12

sth


If you always want a negative value, and you don't know if it'll be positive or negative:

function getNegativeOf(val) {
    return Math.abs(val) * -1;
};

Then use as:

var guaranteedNegativeImageWidth = getNegativeOf(parseInt($('#' + divId).find('#inner-image').css('width'), 10));
like image 21
Matt Avatar answered Dec 30 '22 16:12

Matt