簡體   English   中英

為什么在 if 語句中使用邏輯 AND 時會得到意外標記?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM