X-Git-Url: http://git.scottworley.com/inverse-tax/blobdiff_plain/151ede0ff13d8942d91f13a4f52da8a1971a4296..HEAD:/tax_ui.js diff --git a/tax_ui.js b/tax_ui.js index 4f2b625..d71de9f 100644 --- a/tax_ui.js +++ b/tax_ui.js @@ -1,6 +1,8 @@ -function load_tax_table(table_id, deductible_id) { - return apply_deductible(parse_tax_table(document.getElementById(table_id).value), - parseFloat(document.getElementById(deductible_id).value)); +"use strict"; + +function load_tax_table(table_id, deductible_id = null) { + const deductible = deductible_id == null ? 0.0 : parseFloat(document.getElementById(deductible_id).value); + return apply_deductible(parse_tax_table(document.getElementById(table_id).value), deductible); } function format_number(n) { @@ -8,17 +10,24 @@ function format_number(n) { } function calculate() { - const table1 = load_tax_table('tax_table_1', 'deductible_1'); - const table2 = load_tax_table('tax_table_2', 'deductible_2'); + const tax_tables = [ + load_tax_table('tax_table_1', 'deductible_1'), + load_tax_table('tax_table_2', 'deductible_2'), + load_tax_table('tax_table_3'), + load_tax_table('tax_table_4'), + load_tax_table('tax_table_5') + ]; const after_tax = parseFloat(document.getElementById('after_tax').value); - const before_tax = invert(merge_tax_tables(table1, table2))(after_tax); - - const tax1 = tax(table1, before_tax); - const tax2 = tax(table2, before_tax); + const merged_tax_table = tax_tables.reduce(merge_tax_tables); + const before_tax = invert(merged_tax_table)(after_tax); + const taxes = tax_tables.map(t => tax(t, before_tax)); document.getElementById('before_tax').textContent = format_number(before_tax); - document.getElementById('tax_1').textContent = format_number(tax1); - document.getElementById('tax_2').textContent = format_number(tax2); - document.getElementById('after_tax_verification').textContent = format_number(before_tax - (tax1 + tax2)); + document.getElementById('tax_1').textContent = format_number(taxes[0]); + document.getElementById('tax_2').textContent = format_number(taxes[1]); + document.getElementById('tax_3').textContent = format_number(taxes[2]); + document.getElementById('tax_4').textContent = format_number(taxes[3]); + document.getElementById('tax_5').textContent = format_number(taxes[4]); + document.getElementById('after_tax_verification').textContent = format_number(before_tax - sum(taxes)); }