Switch语句返回错误的结果

时间:2017-03-31 18:31:58

标签: javascript function switch-statement

我试图通过这个函数运行,它接受两个参数,收入和过滤器状态,并使用许多switch语句将收入调整为税后值。我在某处显然犯了一个错误,因为该函数始终返回0而不是我预期的结果。

    var afterTaxes = function(income, filer) {
    var adjusted = 0;
    if (filer === "single") {
        switch (income) {
            case income < 9325:
                adjusted = (1.00 - 0.10) * income;
                break;
            case income < 37950:
                adjusted = (1.00 - 0.15) * income;
                break;
            case income < 91900:
                adjusted = (1.00 - 0.25) * income;
                break;
            case income < 191650:
                adjusted = (1.00 - 0.33) * income;
                break;
            case income < 416700:
                adjusted = (1.00 - 0.35) * income;
                break;
            case income >= 416700:
                adjusted = (1.00 - 0.40) * income;
                break;
            };
    } else if (filer === "married") {
        var adjusted = 0;
        switch (income) {
            case income < 9325:
                adjusted = (1.00 - 0.10) * income;
                break;
            case income < 37950:
                adjusted = (1.00 - 0.15) * income;
                break;
            case income < 91900:
                adjusted = (1.00 - 0.25) * income;
                break;
            case income < 191650:
                adjusted = (1.00 - 0.33) * income;
                break;
            case income < 416700:
                adjusted = (1.00 - 0.35) * income;
                break;
            case income >= 416700:
                adjusted = (1.00 - 0.40) * income;
                break;
        };  
    };
    return adjusted;
};

afterTaxes(150000, "single");

1 个答案:

答案 0 :(得分:0)

假设这是JS,您的switch语句不正确。它需要一个表达式documentation。快速解决方法是将语句更改为var express = require('express'); var session = require('express-session'); var app = express(); app.use(session({ secret : "secretkey", resave : false, saveUninitialized : false })); app.use(passport.initialize()); app.use(passport.session()); //now add path where your passport authentication file is placed. require('./customerConfig/passport');