-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.js
76 lines (73 loc) · 2.8 KB
/
dom.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
75
76
// Variable names list:
// input fileds:
// income: #income; food: #exp-food; rent: #exp-rent; cloth: #exp-cloth;save: #save-percent;
// buttons:
// calculate-button: #calculate; save-button: #save;
// headers:
// total-expense: #exp-total; balance: #balance; saving-amount: #savings; remaining-balance: #remainings;
// function to return an element by id
function element(name) {
return document.getElementById(name);
}
// function to check whether its a string or not
function strcheck(name) {
if (isNaN(element(name).value)) {
return false;
} else if (element(name).value.length < 1)
return false;
else
return true;
}
// function to check whether its a negative number or not
function numcheck(name) {
if (parseFloat(element(name).value) < 0)
return false;
else
return true;
}
// Event for "calculate" button
element('calculate').addEventListener('click', function() {
if (!strcheck('income') || !strcheck('exp-food') || !strcheck('exp-rent') || !strcheck('exp-cloth')) {
element('balance').innerText = 0;
element('exp-total').innerText = 0;
alert('Please enter number');
} else if (!numcheck('income') || !numcheck('exp-food') || !numcheck('exp-rent') || !numcheck('exp-cloth'))
alert('Value cannot be negative');
else {
const income = parseFloat(element('income').value);
const food = parseFloat(element('exp-food').value);
const rent = parseFloat(element('exp-rent').value);
const cloth = parseFloat(element('exp-cloth').value);
const total = food + rent + cloth;
if (total > income) {
alert('expense cannot be greater than income')
element('exp-total').innerText = 0;
element('balance').innerText = 0;
} else {
element('exp-total').innerText = total;
element('balance').innerText = income - total;
}
}
})
// Event for "save" button
element('save').addEventListener('click', function() {
if (!strcheck('save-percent'))
alert('Please enter number');
else if (!numcheck('save-percent'))
alert('Value cannot be negative');
else {
const sval = parseFloat(element('save-percent').value);
const income = parseFloat(element('income').value);
const balance = parseFloat(element('balance').innerText);
const temp = (income / 100) * sval;
if (temp > balance) {
alert('savings cannot be greater than balance');
element('save-percent').value = '';
} else if (temp < 0)
alert('savings cannot be negative');
else {
element('savings').innerText = temp;
element('remainings').innerText = balance - temp;
}
}
})