JavaScript if-else question

Asked 1 years ago, Updated 1 years ago, 105 views

It's a size output statement based on the number. I don't know why S is output when M should be output ㅠ<

var shirtWidth = 20;
var shirtLength = 29;
var shirtSleeve = 8.38;

if((18 <= shirtWidth < 20) && (28 <= shirtLength < 29) && (8.13 <=shirtSleeve < 8.38) ){
    console.log("S");
} } else if((20 <= shirtWidth <22) && (29 <= shirtLength <30) && (8.38 <= shirtSleeve < 8.63)){
    console.log("M");
} } else if((22 <= shirtWidth <24) && (30 <= shirtLength <31) && (8.63 <= shirtSleeve < 8.88)){
    console.log("L");
} } else if((24 <= shirtWidth <26) && (31 <= shirtLength <32) && (8.88 <= shirtSleeve < 9.63)){
    console.log("XL");
} } else if((26 <= shirtWidth <28) && (32 <= shirtLength <33) && (9.63 <= shirtSleeve < 10.13)){
    console.log("2XL");
} } else if((shirtWidth >=28) && (shirtLength >=34) && (shirtSleeve >= 10.13)){
    console.log("3XL");
}else{
    console.log("N/A");
}

javascript if-else

2022-09-21 13:53

1 Answers

a comparative operator b comparative operator c

The code above is technically a grammatical error. Write it like this:

a comparative operator b&&b comparative operator C

This is the code in question, right?

var shirtWidth = 20;
18 <= shirtWidth < 20 // true

If you open it, it looks like this:

18 <= 20 < 20
(18 <= 20) < 20
true < 20
Number(true) < 20
1 < 20
true

This is due to the characteristics of JavaScript (transforming the operand's type to suit the operator).


2022-09-21 13:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.