[英]Why am I getting an unexpected token when using logical AND in an if statement?
我正在尝试使用 if 语句遍历一个数组,该数组中包含具有多个属性值的构造函数的实例。
我正在使用逻辑 AND 运算符来确保满足 2 个条件,但我不断收到 Uncaught SyntaxError 消息:
带有第二个逻辑条件运算符的意外令牌被定位。
我以前用过这个,从来没有遇到过这个问题,所以现在不明白为什么? 我仍在学习 Javascript,但这似乎应该很简单?
我试过删除消息抛出的操作符,但这给我留下了一个条件。
class Streets {
constructor(name, area) {
this.name = name;
this.area = area;
}
}
const street1 = new Streets('Brookwood Glen', 500);
const street2 = new Streets('Abbey Street', 1500);
const street3 = new Streets('Grafton Street', 3000);
const street4 = new Streets('Drury Street', 5000);
const totalStreets = [street1, street2, street3, street4];
function getStreetSize() {
for(let cur of totalStreets) {
if(cur.area > 0 && <= 500) { //This line is where I get the error message
console.log(`${cur.name} has a length of ${cur.size}m and is a tiny street.`);
} else if(cur.area > 500 && =< 1000) {
console.log(`${cur.name} has a length of ${cur.size}m and is a small street.`);
} else if(cur.area > 1000 && =< 1500) {
console.log(`${cur.name} has a length of ${cur.size}m and is a normal street.`);
} else if(cur.area > 1500 && =< 2000) {
console.log(`${cur.name} has a length of ${cur.size}m and is a big street.`);
} else if(cur.area > 2000) {
console.log(`${cur.name} has a length of ${cur.size}m and is a huge street.`);
} else {
console.log(`${cur.name} is a normal street`);
}
}
}
我期待 for 循环遍历 'totalStreets' 数组中的元素并评估 'area' 值是否在 2 个条件之间并将相应的语句打印到控制台,但它不允许我使用小于/ 大于运算符。
您需要在 AND 的每一侧都有一个有效的表达式。
=< 1000
不是有效的表达式,缺少左侧。
您不能从另一个表达式的>
的LHS
暗示=<
的LHS
的值。 您必须明确说明。
cur.area > 500 && cur.area =< 1000
您需要在&&
之后放置cur.area <= 500
您还应该将=<
替换=<
<=
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.