I am trying to compare two negative numbers and the comparison is failing. In this specific case, getLeftPercent() is a negative number (-14) yet when comparing, action B is performed. Logically, (-14) is less than (-10) thus action A should be performed.
If I change the right side comparison operand (-10) to a positive number (1 for example) then action A is performed. Is there some quirk of JavaScript that I'm overlooking (which is staring me in the face)? I can post the complete code but there's not really much more.
$(function() {
// the statement in question
if (parseInt(getLeftPercent(), 10) < -10) {
// do action A
// this is just a debug statement
$('#posbanner').html('element position is less than negative 10');
} else {
// do action B
// this is just a debug statement
$('#posbanner').html('element position is greater than or equal to negative 10');
}
});
// element left position on page
function getLeftPercent() {
var leftStr = $('#sidebar').css('left'),
pxPos = leftStr.indexOf('px'),
leftVal = leftStr.substr(0, pxPos),
leftPercent = (leftVal / $(window).width() * 100).toString(),
dotPos = leftPercent.indexOf('.'),
leftPercentStr = dotPos == -1 ? leftPercent : leftPercent.substr(0, dotPos);
return leftPercentStr;
};
getLeftPercent()?console.log(getLeftPercent())and check the result in your console (Ctrl+Shift+J in Chrome/Firefox)