我的功能遇到问题。当我输入calculateTaxRate(10000,“joint”)时,它没有给我10%的正确答案。它返回“最好调用会计师”。我不知道为什么这种情况仍然发生。任何向我解释这一点的帮助将不胜感激。
function calculateTaxRate(salary, status) {
if (status !== ("single" || "joint") || (salary > 74900)) {
return "Better call an accountant";
} else if (status == "single") {
if (salary <= 9225) {
return "10%";
} else if (9226 <= salary && salary <= 37450) {
return "15%";
} else {
return "25%";
}
}
if (status == "joint") {
if (0 <= salary && salary <= 18450) {
return "10%";
} else if (18451 <= salary && salary <= $74, 900) {
return "15%";
}
}
}
最佳答案
代码("single"|| "joint")
计算结果为“single”。
OR 条件写为 expr1 || expr2
如果可以转换为 true,则返回 expr1;否则,返回 expr2。例如:
true || false = true
false || true = true
"Single" || false = "Single"
false || "Joint" = "Joint"
IF条件应写为:
if ((status !== "single" && status !== "joint") || (salary > 74900)) {
请参阅 Logical Operators 上的 Mozilla 开发人员文档
关于javascript - 税务功能问题(If/Else 语句),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40251214/