-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin-sal-calc.js
74 lines (62 loc) · 2.72 KB
/
min-sal-calc.js
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
const inputSalary = document.getElementById("gross_annual");
const inputExpense = document.getElementById("monthly_expense");
const inputTax = document.getElementById("estimated_taxes");
function isNumber(meinVal){
//checks if it's a number
return !isNaN(parseFloat(meinVal)) && isFinite(meinVal);
}
function isValid(meinValue){
//returns true if valid
if(meinValue != "" && isNumber(meinValue) == true){
return true;
} else {
return false;
}
}
document.getElementById("submit_button").addEventListener("click", function(){
var answer;
if(isValid(inputSalary.value) && isValid(inputExpense.value) && isValid(inputTax.value)){
//subtract the percentage entered, then multiply by .01
var taxMath = (100 - inputTax.value) * .01;
//multiply the taxMath result with the gross annual salary
var netAnnual = inputSalary.value * taxMath;
//divide the net annual salary by 12
var monthlyNet = netAnnual / 12;
//if the net monthly amount is bigger than the monthly expense total
//then it will not be enough
let usDollar = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD'});
formattedMonthlyNet = usDollar.format(monthlyNet);
formattedInputExpense = usDollar.format(inputExpense.value);
answer = (formattedMonthlyNet > formattedInputExpense) ? "YES!" : "NO.";
numberAnswer = formattedMonthlyNet - formattedInputExpense;
theMath = document.getElementById("annual_div_twelve");
theMath.innerHTML = 'If (Net Annual Salary divided by 12) is ' + formattedMonthlyNet + ' and (Monthly Expense Total) is ' + formattedInputExpense + ', then the answer is:';
mExNetAnnual = inputExpense.value * 12;
taxToAdd = mExNetAnnual * taxMath;
neededTotal = mExNetAnnual + taxToAdd;
formattedNeed = usDollar.format(neededTotal);
theResult = document.getElementById("result");
//return the answer here
theResult.innerHTML = answer;
if(answer == "NO."){
theNeed = document.getElementById("need");
theNeed.innerHTML = 'You would need ' + formattedNeed;
} else {
theNeed.innerHTML = "";
}
} else {
alert("Only numbers, please.");
}
});
document.getElementById("clear_button").addEventListener("click", function(){
//clear all input boxes
inputSalary.value = "";
inputExpense.value = "";
inputTax.value = "";
theNeed = document.getElementById("need");
theNeed.innerHTML = "";
theResult = document.getElementById("result");
theResult.innerHTML = "";
theMath = document.getElementById("annual_div_twelve");
theMath.innerHTML = "";
});