首页 > 解决方案 > 如何打印报表

问题描述

我试图让函数打印符合代码的语句,但它是说一些未表示的数字正在打印大小而不是 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");
    
    }

预计 shirtWidth 为 18,shirtLength 为 29,shirtSleeve 为 8.47 以记录尺寸 N/A,但收到 S

标签: javascriptif-statement

解决方案


例如,当使用 OR 运算符a===1 || b===1时,当两者之一为真时,条件为真。

所以当你说:

if (shirtWidth === 18 || ...) {
  console.log("S");
}

||当 shirtWidth = 18 时,将不会查看您背后的所有代码。

还要记住 and 的优先级,&&即使||不需要括号,放置括号也很有用:

if (shirtWidth===18 
    || (shirtWidth < 20 && shirtLength === 28)
    || (shirtLength < 29 && shirtSleeve === 8.13)
    || shirtSleeve < 8.38)

推荐阅读