Skip to content

Commit b99fda5

Browse files
committed
Fix: resolve issue thinkswell#1164
1 parent 27ee633 commit b99fda5

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

Calculator/jashkarangiya/index.html

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
<table>
2525
<tr>
2626
<td><button class="btn-operator" id="clear">C</button></td>
27-
<td><button class="btn-operator" id="/">&divide;</button></td>
28-
<td><button class="btn-operator" id="*">&times;</button></td>
29-
<td><button class="btn-operator" id="backspace"><</button></td>
27+
<td><button class="btn-operator" id="/">/</button></td>
28+
<!-- <td><button class="btn-operator" id="*">&times;</button></td> -->
29+
<td><button class="btn-operator" id="*">*</button></td>
30+
<td><button class="btn-operator" id="backspace">
31+
< </button>
32+
</td>
3033
</tr>
3134
<tr>
3235
<td><button class="btn-number" id="7">7</button></td>

Calculator/jashkarangiya/js/index.js

+37-18
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,48 @@ const display = document.querySelector("#display");
22
const buttons = document.querySelectorAll("button");
33

44
buttons.forEach((item) => {
5-
item.onclick = () => {
6-
if (item.id == "clear") {
7-
display.innerText = "";
8-
} else if (item.id == "backspace") {
9-
let string = display.innerText.toString();
10-
display.innerText = string.substr(0, string.length - 1);
11-
} else if (display.innerText != "" && item.id == "equal") {
12-
display.innerText = eval(display.innerText);
13-
} else if (display.innerText == "" && item.id == "equal") {
14-
display.innerText = "Empty!";
15-
setTimeout(() => (display.innerText = ""), 2000);
16-
} else {
17-
display.innerText += item.id;
5+
item.onclick = () => {
6+
if (item.id == "clear") {
7+
display.innerText = "";
8+
} else if (item.id == "backspace") {
9+
let string = display.innerText.toString();
10+
display.innerText = string.substr(0, string.length - 1);
11+
} else if (display.innerText != "" && item.id == "equal") {
12+
try {
13+
if (display.innerText.includes("/0")) {
14+
throw new Error("Division by zero");
1815
}
19-
};
16+
display.innerText = Function(
17+
'"use strict";return (' + display.innerText + ")"
18+
)();
19+
} catch (e) {
20+
display.innerText = "Cannot divide by zero!";
21+
setTimeout(() => (display.innerText = ""), 2000);
22+
}
23+
} else if (display.innerText == "" && item.id == "equal") {
24+
display.innerText = "Empty!";
25+
setTimeout(() => (display.innerText = ""), 2000);
26+
} else {
27+
const lastChar = display.innerText.slice(-1);
28+
if (
29+
["+", "-", "*", "/"].includes(lastChar) &&
30+
["+", "-", "*", "/"].includes(item.innerText)
31+
) {
32+
// Prevent adding multiple consecutive operators
33+
display.innerText = display.innerText.slice(0, -1) + item.innerText;
34+
} else {
35+
display.innerText += item.innerText;
36+
}
37+
}
38+
};
2039
});
2140

2241
const themeToggleBtn = document.querySelector(".theme-toggler");
2342
const calculator = document.querySelector(".calculator");
2443
const toggleIcon = document.querySelector(".toggler-icon");
2544
let isDark = true;
2645
themeToggleBtn.onclick = () => {
27-
calculator.classList.toggle("dark");
28-
themeToggleBtn.classList.toggle("active");
29-
isDark = !isDark;
30-
};
46+
calculator.classList.toggle("dark");
47+
themeToggleBtn.classList.toggle("active");
48+
isDark = !isDark;
49+
};

0 commit comments

Comments
 (0)