]> git.scottworley.com Git - inverse-tax/blob - tax_ui.js
1148f54246986fa27fe86d349f9d9a96af710e1a
[inverse-tax] / tax_ui.js
1 "use strict";
2
3 function load_tax_table(table_id, deductible_id = null) {
4 const deductible = deductible_id == null ? 0.0 : parseFloat(document.getElementById(deductible_id).value);
5 return apply_deductible(parse_tax_table(document.getElementById(table_id).value), deductible);
6 }
7
8 function format_number(n) {
9 return n.toLocaleString('en', {useGrouping:true, style: 'currency', currency: 'USD'});
10 }
11
12 function calculate() {
13 const tax_tables = [
14 load_tax_table('tax_table_1', 'deductible_1'),
15 load_tax_table('tax_table_2', 'deductible_2'),
16 load_tax_table('tax_table_3'),
17 load_tax_table('tax_table_4')
18 ];
19 const after_tax = parseFloat(document.getElementById('after_tax').value);
20
21 const merged_tax_table = tax_tables.reduce(merge_tax_tables);
22 const before_tax = invert(merged_tax_table)(after_tax);
23 const taxes = tax_tables.map(t => tax(t, before_tax));
24
25 document.getElementById('before_tax').textContent = format_number(before_tax);
26 document.getElementById('tax_1').textContent = format_number(taxes[0]);
27 document.getElementById('tax_2').textContent = format_number(taxes[1]);
28 document.getElementById('tax_3').textContent = format_number(taxes[2]);
29 document.getElementById('tax_4').textContent = format_number(taxes[3]);
30 document.getElementById('after_tax_verification').textContent = format_number(before_tax - sum(taxes));
31 }