forked from ThorNayMaan/js-simple-calculator-demo-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
98 lines (83 loc) · 2.88 KB
/
script.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const input = document.querySelector('.input-num');
const clear = document.querySelector('.clear');
const backspace = document.querySelector('.backspace');
const plus = document.querySelector('.plus');
const minus = document.querySelector('.minus');
const equal = document.querySelector('.equal');
const multiplication = document.querySelector('.multiplication');
const division = document.querySelector('.division');
const calNums = document.querySelectorAll('.cal-num');
const inputInfo = document.querySelectorAll('.input-info');
let storedValue = [];
let result = 0;
let calcu = '';
function calcuSignBtnWork(sign) {
if (input.value == '0') {
console.log('lol');
} else {
calcu = sign;
if (result == 0) {
storedValue.push(+input.value);
} else {
storedValue = [+input.value];
result = 0;
}
input.value = '';
inputInfo[0].childNodes[0].data =
'- ' + storedValue[storedValue.length - 1] + ' -';
}
}
function resultFunc() {
input.value = result;
storedValue = [result];
calcu = '';
inputInfo[0].childNodes[0].data =
'- ' + storedValue[storedValue.length - 1] + ' -';
}
calNums.forEach(n => {
n.addEventListener('click', e => {
if (input.value.startsWith('0')) input.value = ''; //to reset the input: remove zero
input.value += +e.target.innerText;
});
});
equal.addEventListener('click', () => {
if (calcu === 'substraction') {
storedValue.push(+input.value);
result = storedValue.reduce((a, n) => a - n);
resultFunc();
}
if (calcu === 'addition') {
storedValue.push(+input.value);
result = storedValue.reduce((a, n) => a + n); //only this way: not turn into NaN
resultFunc();
}
if (calcu === 'multiplication') {
storedValue.push(+input.value);
result = storedValue.reduce((a, n) => a * n);
resultFunc();
}
if (calcu === 'division') {
storedValue.push(+input.value);
result = storedValue.reduce((a, n) => a / n);
resultFunc();
}
});
plus.addEventListener('click', () => {
calcuSignBtnWork('addition');
});
minus.addEventListener('click', () => {
calcuSignBtnWork('substraction');
});
multiplication.addEventListener('click', () => {
calcuSignBtnWork('multiplication');
});
division.addEventListener('click', () => {
calcuSignBtnWork('division');
});
clear.addEventListener('click', () => {
input.value = '0'; //only reset the visually input value: not storedValue var
});
backspace.addEventListener('click', () => {
input.value = input.value.slice(0, input.value.length - 1);
});
// BUG: There's an issue with consequence calculation; meaing operation does only on the lastest operator, although 'plus' went before 'minus', all the inputed number will be operation on the lastest 'minus' and has skipped the 'plus'.