2017-03-31 100 views
0

第一次海報 - 我試圖運行這個函數,它帶有兩個參數,收入和過濾器狀態,並使用一些switch語句將收入調整到稅後值。我明顯在某處犯了一個錯誤,因爲函數始終返回0而不是我的預期結果。Switch語句返回錯誤結果

所有建議表示讚賞!

感謝, 亞當

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"); 
+2

這是什麼編程語言? –

回答

0

假設這是JS,你的switch語句不正確。它需要一個表達式見documentation。快速修復是將語句更改爲switch(true){...}

+0

啊謝謝。你幫我弄清楚我出錯的地方。 – Adam