javascript - 如何打印报表
问题描述
我试图让函数打印符合代码的语句,但它是说一些未表示的数字正在打印大小而不是 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
解决方案
例如,当使用 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)
推荐阅读
- java - 如何在特定字符串后获得更多组匹配?
- javascript - 如何在javascript中以数组的形式返回对象的特定属性
- python - Django中具有默认选择的单选按钮
- c# - 为什么 HttpRequest 不能在 Windows 上工作,但在 Mac 上工作?
- android - Android XMPPTCPConnection:机器不在网络上
- smartcard - 为什么CLA=C0类不能再自己用了?
- laravel - 如何在 laravel 5.8 上使用不同的会话设置默认重定向
- swift - 使用 SWIFT 从 MVC 中的控制器更新 viewController
- pandas - 多条件分组和计数 pandas
- firebase - 如何在函数中包含存储服务帐户密钥