admin管理员组文章数量:1390609
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;
}
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;
}
Share
Improve this question
asked Aug 7, 2015 at 0:50
Tony BrasunasTony Brasunas
4,6213 gold badges47 silver badges61 bronze badges
2
- Please add a jsfiddle – Ray Commented Aug 7, 2015 at 1:03
- JavaScript will ignore the 0 in front of the zip – DylanB Commented Aug 7, 2015 at 1:04
2 Answers
Reset to default 903000
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 parisons, why not omit the leading zero in your parison?
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/en-US/docs/Web/JavaScript/Guide/Grammar_and_types https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
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/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
However that's not the only thing at play here. If you were only paring 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 parison 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.
本文标签: Why doesn39t gt (greater than or equals) comparison work in JavascriptStack Overflow
版权声明:本文标题:Why doesn't >= (greater than or equals) comparison work in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744657835a2618071.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论