/
main.js
146 lines (117 loc) · 3.54 KB
/
main.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
const numberButtons = document.querySelectorAll('.btn-num');
const operateButtons = document.querySelectorAll('.btn-op');
const clearButton = document.querySelector('#btn-clear');
const deleteButton = document.querySelector('#btn-delete');
const equalButton = document.querySelector('#btn-equal');
const dotButton = document.querySelector('#btn-dot');
const topScreen = document.querySelector('#top-text');
const bottomScreen = document.querySelector('#bottom-text');
const topDisplay = document.querySelector('#screen-top');
const bottomDisplay = document.querySelector('#screen-bottom');
let overwrite = true;
let operand1 = "";
let operand2 = "";
let operation = "";
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
return a / b;
}
function operate(operator, a, b) {
switch (operator) {
case "+":
return add(a, b);
case "-":
return subtract(a, b);
case "x":
return multiply(a, b);
case "÷":
if (b === 0) {
alert("Error: dividing with zero");
return;
}
return divide(a, b);
}
}
function enterValue(e) {
if (bottomScreen.textContent.length === 16 && overwrite === false) {
return;
}
let value =e.target.textContent;
console.log(value);
if (bottomScreen.textContent === "0" && value === "0") {
return;
}
console.log(overwrite)
if (overwrite === true) {
bottomScreen.textContent = value;
overwrite = false;
} else {
bottomScreen.textContent += value;
}
}
function callOperation(e) {
overwrite = true;
let value= e.target.textContent;
// console.log(operand1.length);
// console.log(operand2.length);
if (operand1.length === 0) {
operation = value;
// console.log(operation)
operand1 = bottomScreen.textContent;
topScreen.textContent = operand1 + " " + operation;
} else if (operand2.length != 0) {
operation = value;
topScreen.textContent = bottomScreen.textContent + " " + operation;
operand1 = bottomScreen.textContent;
operand2 = "";
}else {
let result = operate(operation, Number(operand1), Number(bottomScreen.textContent));
if (result % 1 != 0) {
result = result.toFixed(4);
}
operand1 = result;
operation = value;
topScreen.textContent = operand1 + " " + operation;
bottomScreen.textContent = operand1;
}
}
function clear() {
overwrite = true;
bottomScreen.textContent = '0';
topScreen.textContent = '';
operand1 = "";
operand2 = "";
operation = "";
}
function deleteValue() {
if (bottomScreen.textContent.length > 0) {
bottomScreen.textContent = bottomScreen.textContent.slice(0, -1);
}
}
function evaluateExpression() {
operand2 = bottomScreen.textContent;
if (operand1.length != 0 && operand2.length != 0 && operation.length != 0) {
let result = operate(operation, Number(operand1), Number(operand2));
if (result % 1 != 0) {
result = result.toFixed(4);
}
topScreen.textContent += (" " + operand2 + " =");
bottomScreen.textContent= result
}
}
/* Event listeners */
numberButtons.forEach(b => {
b.addEventListener('click', enterValue);
});
operateButtons.forEach(button => {
button.addEventListener('click', callOperation);
});
equalButton.addEventListener('click', evaluateExpression);