Tony Brasunas
Tony Brasunas

Reputation: 4582

Why doesn't >= (greater than or equals) comparison work in Javascript?

What am I missing here? This script looks right to me.

But for some reason when I send it a zipcode of 02897 (or anything that should be Rhode Island), it returns New Hampshire. Aside from political beliefs Javascript developers may have (sure most people would prefer to live in New Hampsire than Rhode Island), why doesn't this script work?

New Jersey and Alabama work fine. Why can't Rhode Island get some love?

function getState(zip) {
    var thiszip = zip; // parseInt(zip);
    if (thiszip >= 35000 && thiszip <= 36999) {
            thisst = 'AL';
            thisstate = "Alabama";
            }
    else if (thiszip >= 03000 && thiszip <= 03899) {
        thisst = 'NH';
        thisstate = "New Hampshire";
        }
    else if (thiszip >= 07000 && thiszip <= 08999) {
        thisst = 'NJ';
        thisstate = "New Jersey";
        } 
    else if (thiszip >= 02800 && thiszip <= 02999) {
        thisst = 'RI';
        thisstate = "Rhode Island";
        }
    else {
        thisst = 'none';
    }
   return thisst;
}

Upvotes: 3

Views: 783

Answers (2)

Kevin Boucher
Kevin Boucher

Reputation: 16675

03000 equals 1536 as a decimal.

This is because the leading zero causes the value to be interpreted as an octal.

Since in the end you are doing number value comparisons, why not omit the leading zero in your comparison?

else if (thiszip >= 3000 && thiszip <= 3899) {

otherwise, use parseInt and declare decimal:

else if (thiszip >= parseInt(03000, 10) && thiszip <= parseInt(03899, 10)) {
                                 // ^^^ pass radix parameter          ^^^ pass radix parameter

And you probably want to parseInt the value being passed in:

var thiszip = parseInt(zip, 10);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

Upvotes: 9

abaldwin99
abaldwin99

Reputation: 913

If a number starts with zero in javascript it can be treated as octal.

Quote from the following docs...

If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

However that's not the only thing at play here. If you were only comparing octals cast as decimals you would get the output you expect. In chrome 43 passing in a number that cannot be converted to octals (any digit has an 8 or 9) will keep it as a base 10.

That is probably why if statements before Rhode Island gave you the output you expected. For Example you might have passed in a zip of 03274 expecting New Hampshire and you would get what you expect. The if statement is actually doing this...

03274 >= 03000 && 03274 <= 03899

converts to...

1724 >= 1536 && 1724 <= 3899

However when you pass in 02897 expecting Rhode Island the logic is going to evaluate to True on the New Hampshire if statement. Here's what the actual comparison is...

02897 >= 03000 & 02897 <= 03899

converts to....

2897 >= 1536 && 2897 <= 3899

See Kevin's answers for how to actually fix the function to work as intended.

Upvotes: 4

Related Questions