]> git.scottworley.com Git - inverse-tax/blame - tax_ui.js
README
[inverse-tax] / tax_ui.js
CommitLineData
2d75ab3b
SW
1"use strict";
2
e6606316
SW
3function 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);
d0678fe5
SW
6}
7
8function format_number(n) {
9 return n.toLocaleString('en', {useGrouping:true, style: 'currency', currency: 'USD'});
10}
11
12function calculate() {
59179b68
SW
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'),
899824d5
SW
17 load_tax_table('tax_table_4'),
18 load_tax_table('tax_table_5')
59179b68 19 ];
d0678fe5
SW
20 const after_tax = parseFloat(document.getElementById('after_tax').value);
21
59179b68
SW
22 const merged_tax_table = tax_tables.reduce(merge_tax_tables);
23 const before_tax = invert(merged_tax_table)(after_tax);
24 const taxes = tax_tables.map(t => tax(t, before_tax));
d0678fe5
SW
25
26 document.getElementById('before_tax').textContent = format_number(before_tax);
59179b68
SW
27 document.getElementById('tax_1').textContent = format_number(taxes[0]);
28 document.getElementById('tax_2').textContent = format_number(taxes[1]);
29 document.getElementById('tax_3').textContent = format_number(taxes[2]);
30 document.getElementById('tax_4').textContent = format_number(taxes[3]);
899824d5 31 document.getElementById('tax_5').textContent = format_number(taxes[4]);
59179b68 32 document.getElementById('after_tax_verification').textContent = format_number(before_tax - sum(taxes));
d0678fe5 33}