-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
171 lines (120 loc) · 4.78 KB
/
app.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
let display = document.getElementById('display');
let resultDisplay = document.getElementById('displayResult')
let allButtons = document.querySelectorAll('.button');
/* ARRAY OF ELEMENTS */
const numbersArray = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const symbolsArray = ['/', 'x', '-', '+'];
/* ARRAY OF TOTALS */
let total = [];
let partialTotal = [];
/* STATES */
const states = {
equal: true,
};
function count(numb) {
if (Number.isInteger(numb)) {
return 0;
} else {
return numb.toString().split('.')[1].length;
}
};
function roundNum(n) {
if(count(n) >= 3){
return parseFloat((n).toFixed(3));
} else if(count(n) < 3) {
return n;
}
};
function calculator(n) {
let content = n.textContent;
const endOfArr = (n) => n[n.length - 1];
const calculateTwoNum = (n, e) => {
if(e == '+'){
return roundNum((n[0] + endOfArr(n)));
} else if(e == '-') {
return roundNum(n[0] - endOfArr(n));
} else if(e == 'x') {
return roundNum(n[0] * endOfArr(n));
} else if(e == '/') {
return roundNum(n[0] / endOfArr(n));
}
};
const allLogs = () => {
return (`resultDisplay = ${resultDisplay.textContent},
display = ${display.textContent},
total = ${total}
partialTotal = ${partialTotal},
Equal state = ${states['equal']}`)
};
const clear = () => {
total = [];
partialTotal = [];
display.innerText = '';
resultDisplay.innerText = '';
states['equal'] = true;
};
if(content === 'CLEAR'){
clear();
} else if(numbersArray.includes(content) && display.textContent != '0' || content == '.' && endOfArr(partialTotal) !== '.' && !partialTotal.includes('.') && display.textContent !== ''){
if(resultDisplay.textContent == '') {
partialTotal.push(content)
display.innerText = partialTotal.join('');
console.log(allLogs());
} else if(resultDisplay.textContent !== '' && states['equal'] == true) {
partialTotal.push(content);
display.innerText = partialTotal.join('');
console.log(allLogs());
} else if(resultDisplay.textContent !== '' && states['equal'] == false && total.length == 3 && content !== '.') {
console.log(allLogs());
total = [];
total.push(parseFloat(display.textContent))
display.innerText = '';
partialTotal.push(content);
display.innerText = partialTotal.join('');
resultDisplay.innerText = total.join('');
states['equal'] = true;
console.log(allLogs());
}
} else if(symbolsArray.includes(content)) {
if(resultDisplay.textContent == '' && display.textContent !== '') {
partialTotal = [];
total.push(parseFloat(display.innerText))
total.push(content)
resultDisplay.innerText = total.join('')
display.textContent = '';
console.log(allLogs());
}else if (resultDisplay.textContent !== '' && states['equal'] == false && total.length == 3) {
partialTotal = [];
total = [];
total.push(parseFloat(display.innerText))
total.push(content)
resultDisplay.innerText = total.join('')
display.textContent = '';
states['equal'] = true;
console.log(allLogs());
} else if(resultDisplay.textContent !== '' && states['equal'] == true && display.textContent !== '' && !symbolsArray.includes(endOfArr(total))) {
partialTotal = [];
total.push(content)
total.push(parseFloat(display.innerText))
resultDisplay.innerText = total.join('')
display.textContent = calculateTwoNum(total, total[1]);;
console.log(allLogs());
states['equal'] = false;
}
} else if(content == '=' && resultDisplay.textContent !== '' && display.textContent !== '' && states['equal'] == true) {
if (symbolsArray.includes(endOfArr(total))) {
states['equal'] = false;
let lastSymbol = endOfArr(total);
total.push(parseFloat(display.innerText));
resultDisplay.innerText = total.join('');
display.innerText = calculateTwoNum(total, lastSymbol);
partialTotal = [];
console.log(allLogs()) ;
}
}
}
allButtons.forEach((n) => {
n.addEventListener('click', () => {
return calculator(n);
})
})