JQuery / javascript: negative variable parameter

What is the best way to check the negation 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 check as follows:

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

But I dont know how.

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

Thanks again for any direction you can provide.

+4
source share
3 answers

There should be no problem if you place parentheses around the value you want to hide:

 if (imageMargin == -(numberOfFrames*frameWidth-400) ) ... 
+10
source

If you always need a negative value and you don't know whether it will 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)); 
+3
source

How about subtracting from zero?

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

Source: https://habr.com/ru/post/1339384/


All Articles