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

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

But for some reason, when I send him zipcode 02897 (or whatever it should be in Rhode Island), it returns New Hampshire. Political persuasion aside, Javascript developers might (sure most people would rather live in New Hampshire than Rhode Island) why is this script not working?

New Jersey and Alabama are doing great. Why can't Rhode Island 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;
}

      

+3


source to share


2 answers


03000

1536

as well as a decimal number.

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

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

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

      

otherwise use parseInt

and declare a decimal value:



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

      

And you probably want parseInt

to pass a value:

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

+9


source


If a number starts with zero in javascript, it can be thought of as octal.

Quoting from the following docs ...

If the input string begins with "0", then the base will be eighth (octal) or 10 (decimal). It is the radic that is chosen that depends on the implementation. ECMAScript 5 indicates 10 (decimal) is used, but not all browsers Support this yet. For this reason, always specify the radius when using ParseInt.

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

However, this is not the only thing that plays here. If you were only comparing octal numbers as decimal places, you would get the expected result. In chrome 43, passing to a number that cannot be converted to octal (any digit has an 8 or 9) will contain it as base 10.

This is probably why the instructions if

ahead of Rhode Island gave you the expected result. For example, you could go through zip 03274 waiting for New Hampshire and you get what you expect. The if statement actually does this ...

03274 >= 03000 && 03274 <= 03899

      



converts to ...

1724 >= 1536 && 1724 <= 3899

      

However, when you go to 02897 while waiting for Rhode Island, the logic will evaluate to True in the New Hampshire if statement. Here's an actual comparison ...

02897 >= 03000 & 02897 <= 03899

      

converted to ....

2897 >= 1536 && 2897 <= 3899

      

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

+4


source







All Articles