-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (78 loc) · 2.64 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
const buttonsContainer = document.querySelector('.buttons');
const opDisp = document.querySelector('.op-display');
const disp = document.querySelector('.display');
let operation = '';
let sw = false; // switch to know if an operation is active or not
buttonsContainer.addEventListener('click', function (event) {
// checking if a button was clicked inside the container
if (event.target.tagName === 'BUTTON') {
if (
!isNaN(event.target.innerText) ||
(event.target.innerText === '.' && !disp.innerText.includes('.'))
) {
if (disp.innerText === '0') {
disp.innerText = event.target.innerText;
} else {
if (sw === true) {
disp.innerText = '';
disp.innerText += event.target.innerText;
sw = false;
} else {
disp.innerText += event.target.innerText;
}
}
} else if (event.target.innerText === 'AC') {
disp.innerText = '0';
operation = '';
opDisp.innerText = '';
sw = false;
} else if (event.target.innerText === '+/-' && disp.innerText !== '0') {
if (!disp.innerText.includes('-')) {
disp.innerText = '-' + disp.innerText;
} else {
disp.innerText = disp.innerText.replace('-', '');
}
} else if (
// add and substract
event.target.innerText === '+' ||
event.target.innerText === '-'
) {
if (sw !== true) {
operation += disp.innerText + event.target.innerText;
opDisp.innerText = operation;
sw = true;
disp.innerText = eval(operation.substring(0, operation.length - 1));
} else {
operation = operation.replace(/.$/, event.target.innerText);
}
} else if (event.target.innerText === 'x') {
// multiplication
if (sw !== true) {
operation += disp.innerText + '*';
opDisp.innerText = operation;
sw = true;
disp.innerText = eval(operation.substring(0, operation.length - 1));
} else {
operation = operation.replace(/.$/, '*');
}
} else if (event.target.innerText === '÷') {
// division
if (sw !== true) {
operation += disp.innerText + '/';
opDisp.innerText = operation;
sw = true;
disp.innerText = eval(operation.substring(0, operation.length - 1));
} else {
operation = operation.replace(/.$/, '/');
}
} else if (event.target.innerText === '=') {
// equal button
operation += disp.innerText;
disp.innerText = eval(operation);
opDisp.innerText = operation;
operation = '';
} else if (event.target.innerText === '%') {
disp.innerText = disp.innerText / 100;
}
}
});