税务函数问题(If / Else语句)

时间:2016-10-25 23:13:57

标签: javascript

我的功能有问题。当我输入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%";
    }
}
}

1 个答案:

答案 0 :(得分:2)

代码("single" || "joint")评估为&#34;单&#34;。

如果可以转换为true,则写为expr1 || expr2的OR条件将返回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开发人员文档