2016-10-25 58 views
0

我的功能有问题。当我输入calculateTaxRate(10000,“联合”)时,它不会给我10%的正确答案。它正在返回“更好地致电会计师”。我不确定为什么还在发生。任何帮助向我解释这一点将不胜感激。税务功能问题(If/Else语句)

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%"; 
    } 
} 
} 
+5

'状态==( “单” || “联合”)'没有做你的想法 - 你需要学习JavaScript语法 - 它不是无效的,但你基本上检查'if status!==“single”'only –

回答

2

该代码("single" || "joint")评估为“单”。

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

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

如果条件应该写成:

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

Logical Operators见Mozilla的开发者文档