我正在尝试使用 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”数组中的元素并评估“区域”值是否在两个条件之间并将相应的语句打印到控制台,但它不允许我使用小于/大于运算符。