-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
163 lines (127 loc) · 4.92 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const buttons = document.querySelectorAll(".button-element-container");
const screen = document.querySelector(".display-container");
const ACButton = document.querySelector("#erase-all-sign")
const eraseButton = document.querySelector("#erase-button")
const equalButton = document.querySelector("#equal-button")
initialEventListeners()
function initialEventListeners() {
// appen to the sceen
buttons.forEach(button => {
button.addEventListener("click", () => {
const text = button.textContent || button.innerText;
const textNode = document.createTextNode(text);
screen.appendChild(textNode);
});
});
// AC button
ACButton.addEventListener("click", ()=> {
screen.textContent = ""
})
//erase button
eraseButton.addEventListener("click", () => {
screen.textContent = screen.textContent.slice(0, -1);
});
// equal button
equalButton.addEventListener("click", ()=> {
parse()
})
}
function parse() {
let screenContent = screen.textContent;
// Check for unclosed parentheses
if (screenContent.split('(').length !== screenContent.split(')').length) {
alert("Error: unclosed parenthesis");
return;
}
// Check for incomplete operations
if (/[\+\-\*\/]$/.test(screenContent)) {
alert("Error: incomplete operation");
return;
}
// Check for division by zero
if (/\/0/.test(screenContent)) {
alert("Error: division by zero");
return;
}
screenContent = screen.textContent.split("")
screenContent = handleParenthesis(screenContent)
// normal calculation
for (let i = 0; i < screenContent.length; i++) {
const element = screenContent[i]
if (element === "*" || element === "/") {
i = handleCalculation(screenContent, i)
}
}
screenContent = screen.textContent.split("")
for (let i = 0; i < screenContent.length; i++) {
const element = screenContent[i]
if (element === "+" || element === "-") {
i = handleCalculation(screenContent, i)
}
}
}
function handleParenthesis(screenContent) {
lastParenthesisIndex = 0
// hadle Multiplication and division
for (let i = screenContent.length - 1; i > 0; i--) {
if (screenContent[i] === "(") {
tempLastParenthesisIndex = i
lastParenthesisIndex = i
// multiplication and division
while (screenContent[tempLastParenthesisIndex] !== ")") {
tempLastParenthesisIndex++
if (screenContent[tempLastParenthesisIndex] === "*" || screenContent[tempLastParenthesisIndex] === "/") {
tempLastParenthesisIndex = handleCalculation(screenContent, tempLastParenthesisIndex)
}
}
// addition and subtraction
tempLastParenthesisIndex = i
while (screenContent[tempLastParenthesisIndex] !== ")") {
tempLastParenthesisIndex++
if (screenContent[tempLastParenthesisIndex] === "+" || screenContent[tempLastParenthesisIndex] === "-") {
tempLastParenthesisIndex = handleCalculation(screenContent, tempLastParenthesisIndex)
}
}
screenContent.splice(lastParenthesisIndex, 1)
screenContent.splice(lastParenthesisIndex + 1, 1)
}
}
return screenContent
}
function handleCalculation(screenContent, operatorIndex) {
const nums1 = [];
const nums2 = [];
const operator = screenContent[operatorIndex];
console.log(screenContent.splice(operatorIndex,1))
let negativeIndex = operatorIndex - 1;
// backwards
while (negativeIndex >= 0 && !isNaN(screenContent[negativeIndex]) || screenContent[negativeIndex] === ".") {
nums1.unshift(screenContent[negativeIndex]);
screenContent.splice(negativeIndex, 1)
negativeIndex--;
}
let positiveOperatorIndex = negativeIndex + 1
// upwards
while (positiveOperatorIndex < screenContent.length && !isNaN(screenContent[positiveOperatorIndex]) || screenContent[positiveOperatorIndex] === ".") {
nums2.push(screenContent[positiveOperatorIndex]);
screenContent.splice(positiveOperatorIndex, 1)
}
let result;
switch (operator) {
case "+":
result = parseFloat(nums1.join('')) + parseFloat(nums2.join(''));
break;
case "-":
result = parseFloat(nums1.join('')) - parseFloat(nums2.join(''));
break;
case "*":
result = parseFloat(nums1.join('')) * parseFloat(nums2.join(''));
break;
case "/":
result = parseFloat(nums1.join('')) / parseFloat(nums2.join(''));
break;
}
screenContent.splice(negativeIndex + 1,0, result)
screen.textContent = screenContent.join("")
return negativeIndex + 1
}