1

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;
};
4
  • 1
    What is the actual string value returned by getLeftPercent() ? Commented Aug 20, 2013 at 17:53
  • 2
    add console.log(getLeftPercent()) and check the result in your console (Ctrl+Shift+J in Chrome/Firefox) Commented Aug 20, 2013 at 17:54
  • get firebug for FF and debug it step by step, should be able to spot the issue easily Commented Aug 20, 2013 at 17:55
  • The actual value returned by getLeftPercent() is a number, which is -14. I've tried debugging with no answer. Commented Aug 20, 2013 at 19:06

3 Answers 3

2

I tried running the following code on jsfiddle

if (parseInt( -14, 10) < -10)
{
alert("I am in A");
}
else
{
alert("I am in B");
}

I get I am in A Not sure, if getLeftPercent() is returning the value you expect.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response. The reason for the $('#posbanner').html ~ statement is to display the actual value returned by getLeftPercent(). It is returning a valid number, but for some reason is not being compared properly
0

Your getLeftPercent() function seems way overcomplicated. I'm not sure whether your "left" is relative to the document or parent element, I'll assume document here.

function getLeftPercent() {
    'use strict';
    return ( ($('#sidebar').offset()).left * 100 / $(window).width() );
}

1 Comment

Yes, the function was something that I borrowed from elsewhere. It is a little more than needed but there were other parts that I removed (functionality was more than just returning percentage.
-1

Most likely your substrings return some characters before the number.

alert(parseInt('-14abc', 10)); // -14

alert(parseInt('abc-14', 10)); // NaN

2 Comments

The string used in the substring is from $('#sidebar').css('left'), I don't think it would include other extra characters
The value returned by the function is a number and it does match the positioning of the element on the web page. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.