satyacode
Background
Back to js-conditionals

08-tax-calculator.js

JavaScript
1/**
2 * 💰 Sam's Tax Calculator
3 *
4 * Sam is a freelance graphic designer who dreads tax season every year.
5 * Help Sam by building a tax calculator that uses progressive tax brackets.
6 *
7 * How progressive tax works:
8 *   You don't pay the same rate on ALL your income. Each "slice" of income
9 *   is taxed at its own rate:
10 *
11 *   Bracket 1: $0 – $10,000        → 0%  (tax-free!)
12 *   Bracket 2: $10,001 – $30,000   → 10% (only on the amount ABOVE $10,000)
13 *   Bracket 3: $30,001 – $70,000   → 20% (only on the amount ABOVE $30,000)
14 *   Bracket 4: Over $70,000        → 30% (only on the amount ABOVE $70,000)
15 *
16 * Examples:
17 *   - Income $8,000   → Tax = $0 (all in bracket 1)
18 *   - Income $20,000  → Tax = 10% of ($20,000 - $10,000) = $1,000
19 *   - Income $50,000  → Tax = $2,000 + 20% of ($50,000 - $30,000) = $6,000
20 *   - Income $100,000 → Tax = $2,000 + $8,000 + 30% of ($100,000 - $70,000) = $19,000
21 *
22 * Rules:
23 *   - If income is 0 or negative, return 0
24 *
25 * @param {number} income - Annual income in dollars
26 * @returns {number} Total tax amount owed
27 */
28export function calculateTax(income) {
29   if(income <= 0){
30    return 0
31   }
32
33   let tax = 0
34
35   if(income <= 10000){
36    tax += 0 
37   }
38
39   if(income <= 30000 && income >= 10001){
40    tax = 0.1*(income - 10000)
41   }
42
43   if(income <= 70000 && income >= 30001){
44    tax = 2000 + 0.2 * (income - 30000)
45   }
46
47   if(income > 70000) {
48    tax = 2000 + 8000 + 0.3 * (income - 70000)
49   }
50 return tax
51}
52