-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
100 lines (87 loc) · 3.83 KB
/
script.js
File metadata and controls
100 lines (87 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const taxamo = new Taxamoapi({
apiToken: ''
})
// Country Dropdown
function loadCountrie() {
taxamo.countries().then(response => {
const countrySelect = document.getElementById('countrySelect');
response.data.forEach(country => {
const option = document.getElement('option');
option.value = country.code;
option.textContent - country.name;
countrySelect.appendChild(option);
});
});
}
// Function to select tax bracket
function loadTaxBrackets(countryCode, income) {
taxamo.getTaxBrackets(countryCode).then(response => {
let selectedBracket = null;
//Find and dispaly the tax bracket
response.data.tax_brackets.forEach(bracket => {
if (income >= bracket.min_income && income <= bracket.max_income){
selectedBracket = bracket;
}
});
//Display the bracket
if (selectedBracket) {
taxBracketDiv.textContent = `Tax Bracket: ${selectedBracket.name} - ${selectedBracket.rate}%`
} else {
taxBracketDiv.textContent = 'No tax bracket available for this income.';
}
});
}
//Calculate tax based on income
function calcTax() {
const countryCode = document.getElementById('countrySelect').value;
const income = parseFloat(document.getElementById('income').value);
// Load tax brackets
loadTaxBrackets(countryCode, income)
// Load and Display
setTimeout(() => {
taxamo.calcTax({
countryCode: countryCode,
amount: income
}).then(response => {
const resultDiv = document.getElementById('result');
resultDiv.textContent = `Total tax: ${response.data.tax_amount} ${response.data.currency}`;
});
}, 500); //Delay for loading
}
//Initialise page elements
document.addEventListener('DOMContentLoaded', () => {
loadCountries();
document.getElementById('countrySelect').addEventListener('change', () => {
const income = (parseFloat(document.getElementById('incomeInput').value));
const countryCode = document.getElementById('countrySelect').value;
loadTaxBrackets(countryCode, income);
});
document.getElementById('tax').addEventListener('click', calcTax);
document.getElementById('analyse').addEventListener('click', analyseFinances);
});
function analyseFinances() {
let income = parseFloat(document.getElementById('livingExpenses').value) - taxamo.calcTax({
countryCode: countryCode,
amount: income
})
let livingExpenses = parseFloat(document.getElementById('livingExpenses').value) || 0;
let foodExpenses = parseFloat(document.getElementById('foodExpenses').value) || 0;
let educationExpenses = parseFloat(document.getElementById('educationExpenses').value) || 0;
let entertainmentExpenses = parseFloat(document.getElementById('entertainmentExpenses').value) || 0;
let totalExpenses = livingExpenses + foodExpenses + educationExpenses + entertainmentExpenses;
let savings = income - totalExpenses;
let suggestions = "";
if (savings > totalExpenses) {
suggestions = "You're doing well! Consider investing your savings.";
} else if (savings == totalExpenses) {
suggestions = "You are breaking even. Look into cutting down on non-essential expenses.";
} else {
suggestions = "You're spending more than you're earning. Review your expenses to reduce unnecessary spending.";
}
// Display financial analysis
document.getElementById('analysisOutput').innerHTML = `
<p>Your total monthly expenses are: $${totalExpenses.toFixed(2)}</p>
<p>Your savings after expenses: $${savings.toFixed(2)}</p>
<p>Suggestion: ${suggestions}</p>
`;
}