LearnFrenzy
Sign in Take a free test

Conditional Statements · Conditional Statements Coding Problems · Question 5

Income tax with slab rates in JavaScript Solution, output & explanation

  • Program
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

Compute income tax for this example slab table (each slab taxed at its own rate): up to ₹3,00,000 nil; ₹3–7 lakh 5%; ₹7–10 lakh 10%; ₹10–12 lakh 15%; ₹12–15 lakh 20%; above ₹15 lakh 30%. Print the tax for several incomes.

Solution
main.js
// [upper limit of the slab, rate]
const SLABS = [
  [300000, 0],
  [700000, 0.05],
  [1000000, 0.10],
  [1200000, 0.15],
  [1500000, 0.20],
  [Infinity, 0.30],
];

function incomeTax(income) {
  let tax = 0, lower = 0;
  for (const [upper, rate] of SLABS) {
    if (income <= lower) break;                     // nothing left to tax
    const taxable = Math.min(income, upper) - lower;
    tax += taxable * rate;
    lower = upper;
  }
  return tax;
}

for (const income of [250000, 650000, 1100000, 1800000]) {
  console.log(`Income ₹${income.toLocaleString("en-IN")} → tax ₹${incomeTax(income).toLocaleString("en-IN")}`);
}

Output Verified by running it on Node.js

Income ₹2,50,000 → tax ₹0
Income ₹6,50,000 → tax ₹17,500
Income ₹11,00,000 → tax ₹65,000
Income ₹18,00,000 → tax ₹2,30,000

Only the part of the income inside each slab is taxed at that slab's rate. For ₹11 lakh: 4L × 5% + 3L × 10% + 1L × 15% = 20,000 + 30,000 + 15,000 = 65,000; for ₹18 lakh the five taxed slabs add up to 20,000 + 30,000 + 30,000 + 60,000 + 90,000 = 2,30,000. toLocaleString("en-IN") formats with the Indian lakh grouping.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links