javascript - 如何打印报表

标签 javascript if-statement

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

相关文章:

javascript - 如何使用 console.log (Javascript) 打印函数的输出

c# - MVC Controller if/else 语句

javascript - 如何使用 Promise 和 Angular 实现 Ajax 调用?

javascript - 通过 JavaScript 编辑 CSS 会产生左侧赋值错误

javascript - 在textextjs插件中添加额外数据

javascript - 在新选项卡中显示来自 Azure Blob 存储容器的 PDF 文件

python - 如果函数在匹配来自不同列表的两个用户 ID 时执行函数?

if-statement - 做 "if none of the above"的优雅方式是什么?

javascript - 将 div 设置为固定,直到它与其他元素接触并成为绝对元素。向上滚动时不会恢复为固定状态

c - 删除尾随空白