简体   繁体   中英

Why am I getting an unexpected token when using logical AND in an if statement?

I am trying to loop through an array which holds instances of a constructor with multiple property values in, using an if statement.

I am using the logical AND operator to make sure 2 conditions are met, but I keep getting a message of Uncaught SyntaxError:

Unexpected Token with the second logical condition operator being sited.

I have used this before and never had this problem, so don't understand why now? I'm still learning Javascript, but this seems like it should be pretty straight forward?

I've tried deleting the operator that the message throws up, but this leaves me with one condition.

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`);
    }
}
}

I'm expecting the for loop to loop through the elements in the 'totalStreets' array and evaluate if the 'area' value is between the 2 conditions and print the corresponding statement to the console, but it is not letting me use the less than/greater than operators.

You need to have a valid expression on each side of the AND.

=< 1000 is not a valid expression, the Left-Hand Side is missing.

You cannot imply the value for the LHS of the =< from the LHS of the > on the other expression. You must state it explicitly.

cur.area > 500 && cur.area =< 1000

You need to place cur.area <= 500 after the &&

You should also replace =< with <=

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM