JS区间判断的问题

错误的写法:0 < i < 100

正确的写法:0 < i && i < 100

常年写JAVA后端,习惯了区间判断使用 “0 < x < 100”这种形式,今天在JS中使用区间判断发现出问题了

if(Math.round(i/custList.length*100) <= 20) {

            iconPng = path+"/icdc/crpt/css/images/bmap/1.png";

} else if (20 < Math.round(i/custList.length*100) <= 40) {

            iconPng = path+"/icdc/crpt/css/images/bmap/2.png";

} else if (40 < Math.round(i/custList.length*100) <= 60) {

            iconPng = path+"/icdc/crpt/css/images/bmap/3.png";

} else if (60 < Math.round(i/custList.length*100) <= 80) {

            iconPng = path+"/icdc/crpt/css/images/bmap/4.png";

} else if (80 < Math.round(i/custList.length*100) <= 100) {

            iconPng = path+"/icdc/crpt/css/images/bmap/5.png";

}

百度地图显示标注,按排序后百分比显示不同标注颜色,使用上面代码只显示1.png和2.png两种,折腾半天才发现原来是要分开判断,

例如20 < Math.round(i/custList.length*100) <= 40

如果不分开判断,可能就只取20 < Math.round(i/custList.length*100)这部分,所以造成了上述现象,改成如下代码后恢复正常

if(Math.round(i/custList.length*100) <= 20) {

            iconPng = path+"/icdc/crpt/css/images/bmap/1.png";

} else if (20 < Math.round(i/custList.length*100) && Math.round(i/custList.length*100) <= 40) {

            iconPng = path+"/icdc/crpt/css/images/bmap/2.png";

} else if (40 < Math.round(i/custList.length*100) && Math.round(i/custList.length*100) <= 60) {

            iconPng = path+"/icdc/crpt/css/images/bmap/3.png";

} else if (60 < Math.round(i/custList.length*100) && Math.round(i/custList.length*100) <= 80) {

            iconPng = path+"/icdc/crpt/css/images/bmap/4.png";

} else if (80 < Math.round(i/custList.length*100) && Math.round(i/custList.length*100) <= 100) {

            iconPng = path+"/icdc/crpt/css/images/bmap/5.png";

}

猜你喜欢

转载自blog.csdn.net/cjw506730104/article/details/85089577
今日推荐