-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
220 lines (163 loc) · 5.31 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const screen = document.querySelector(".calculator-screen");
const clear = document.querySelector('.clear');
const posNeg = document.querySelector('.pos-neg');
const decimal = document.querySelector('.decimal');
const percentage = document.querySelector('.percentage');
const answerH1 = document.querySelector('.answer');
let currentScreenValue = "";
let previousScreenValue = "";
let operation = "";
// show on screen
function display(){
screen.childNodes[1].textContent = currentScreenValue;
}
// Display pushed buttons on screen
function writeOnScreen() {
document.querySelectorAll(".display").forEach( (event) => {
event.addEventListener('click', (e) => {
if ( (e.target.innerText !== "+/-") || (e.target.innerText !=="%") ){
answerH1.style.display = 'none';
}
// Max number of digits is 27
if (screen.childNodes[1].textContent.length === 27){
return false;
}
// Check if decimal was already entered and if it is chosen again.
if (decimalEntered(e)){
return 1;
}
// Insert Negative
if (inserNagative(e)){
return 1;
}
// Turn percentage
if (turnPercent(e)){
return 1;
}
screen.childNodes[1].textContent += e.target.innerText;
});
});
}
writeOnScreen();
// Check if decimal has already been entered, then do not enter again.
function decimalEntered(e){
if ( (screen.childNodes[1].textContent).includes(".") && (e.target.innerText === '.')){
return true;
}
}
// Insert Negative
function inserNagative(e){
if (e.target.innerText === '+/-'){
if (screen.childNodes[1].textContent !==""){
screen.childNodes[1].textContent *= -1;
}
else {
screen.childNodes[1].textContent = previousScreenValue * -1;
answerH1.style.display = 'none'
previousScreenValue = '';
}
return true;
}
}
// Turn percentage
function turnPercent(e){
if (e.target.innerText === '%'){
if (screen.childNodes[1].textContent !==""){
screen.childNodes[1].textContent /= 100;
}
else {
screen.childNodes[1].textContent = previousScreenValue / 100;
answerH1.style.display = 'none'
previousScreenValue = '';
}
return true;
}
}
// Clear screen whenever clear button or any of the operation buttons are pressed.
// Also store the value from screen.
function storeValues() {
document.querySelectorAll('.operation-btn').forEach( (btn) => {
btn.addEventListener('click', () => {
if (previousScreenValue === ""){
previousScreenValue = screen.childNodes[1].textContent;
screen.childNodes[1].textContent = '';
} else {
currentScreenValue = screen.childNodes[1].textContent;
// Prevent NAN when selecting another operation.
if (currentScreenValue === ""){
return 1;
}else{
operate(operation, currentScreenValue, previousScreenValue);
}
}
})
});
}
storeValues();
// Clear data
let clearData = () => clear.addEventListener('click', () => {
screen.childNodes[1].textContent = "";
previousScreenValue = "";
currentScreenValue = "";
answerH1.style.display = 'none';
operation = "";
})
clearData()
// get operator
function getOperator(){
document.querySelectorAll('.operation-btn').forEach( btn => {
btn.addEventListener('click', (e) => {
operation = e.target.textContent;
});
});
}
getOperator();
// Caculator Operation
function operate(operationSelected, currentVal, prevVal){
switch (operationSelected){
case '+':
addNumbers(prevVal, currentVal);
break;
case 'x':
multiplyNumbers(prevVal, currentVal);
break;
case '-':
subtractNumbers(prevVal, currentVal);
break;
case '÷':
divideNumbers(prevVal, currentVal);
break;
case '+/-':
makeNegative(currentVal);
break;
}
}
operate();
// Add
function addNumbers(val1, val2){
answerH1.textContent = parseFloat(val1) + parseFloat(val2);
previousScreenValue = parseFloat(answerH1.textContent);
answerH1.style.display = "block";
screen.childNodes[1].textContent = previousScreenValue;
}
// Multiply
function multiplyNumbers(val1, val2){
answerH1.textContent = parseFloat(val1) * parseFloat(val2);
previousScreenValue = parseFloat(answerH1.textContent);
answerH1.style.display = "block";
screen.childNodes[1].textContent = "";
}
// Subtract
function subtractNumbers(val1, val2){
answerH1.textContent = parseFloat(val1) - parseFloat(val2);
previousScreenValue = parseFloat(answerH1.textContent);
answerH1.style.display = "block";
screen.childNodes[1].textContent = "";
}
// Divide
function divideNumbers(val1, val2){
answerH1.textContent = parseFloat(val1) / parseFloat(val2);
previousScreenValue = parseFloat(answerH1.textContent);
answerH1.style.display = "block";
screen.childNodes[1].textContent = "";
}