我试图让函数打印符合代码的语句,但它说一些未表示的数字正在打印尺寸而不是 N/A
我用过||或和&&
var shirtWidth = 23;
var shirtLength = 30;
var shirtSleeve = 8.71;
// Write your if/else code here
if (shirtWidth===18 || shirtWidth <20 && shirtLength===28 || shirtLength < 29 && shirtSleeve===8.13 || shirtSleeve <8.38 ){
console.log("S");
}else if (shirtWidth===20 || shirtWidth<22 && shirtLength===29 || shirtLength <30 && shirtSleeve===8.38 || shirtSleeve <8.63){
console.log("M");
}else if (shirtWidth===22 || shirtWidth <24 && shirtLength===30 || shirtLength <31 && shirtSleeve===8.63 || shirtSleeve < 8.88){
console.log("L");
}else if (shirtWidth===24 || shirtWidth <26 && shirtLength===31 || shirtLength < 33 && shirtSleeve===8.88 || shirtSleeve < 9.63){
console.log("XL");
}else if (shirtWidth===26 || shirtWidth < 28 && shirtLength===33 || shirtLength<34 && shirtSleeve===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");
}
记录尺寸的预期衬衫宽度为 18、衬衫长度为 29、衬衫袖子为 8.47 N/A,但收到了 S
最佳答案
当使用 OR 运算符时,例如a===1 || b===1
,当两者之一为真时,条件为真。
所以当你说:
if (shirtWidth === 18 || ...) {
console.log("S");
}
当衬衫宽度 = 18 时,将不会查看 ||
后面的所有代码。
还要记住 &&
和 ||
的优先级,即使不需要,放置括号也很有用:
if (shirtWidth===18
|| (shirtWidth < 20 && shirtLength === 28)
|| (shirtLength < 29 && shirtSleeve === 8.13)
|| shirtSleeve < 8.38)
关于javascript - 如何打印报表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57615479/