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

Issue with Tax Function (If/Else statements)

本文关键字:语句 问题 Else 函数 If 税务      更新时间:2023-09-26

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

写为expr1 || expr2的OR条件返回expr1,如果它可以转换为true;否则,返回expr2。例如:

true || false = true
false || true = true
"Single" || false = "Single"
false || "Joint" = "Joint"

IF条件应该写成:

if ((status !== "single" && status !== "joint") || (salary > 74900)) {

参见Mozilla开发人员文档中的逻辑运算符