Skip to content

Fix: resolve issue #1164 project javaScriptMiniProject/Calculator/jashkarangiya #1180

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Calculator/jashkarangiya/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
<table>
<tr>
<td><button class="btn-operator" id="clear">C</button></td>
<td><button class="btn-operator" id="/">&divide;</button></td>
<td><button class="btn-operator" id="*">&times;</button></td>
<td><button class="btn-operator" id="backspace"><</button></td>
<td><button class="btn-operator" id="/">/</button></td>
<!-- <td><button class="btn-operator" id="*">&times;</button></td> -->
<td><button class="btn-operator" id="*">*</button></td>
<td><button class="btn-operator" id="backspace">
< </button>
</td>
</tr>
<tr>
<td><button class="btn-number" id="7">7</button></td>
Expand Down
55 changes: 37 additions & 18 deletions Calculator/jashkarangiya/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,48 @@ const display = document.querySelector("#display");
const buttons = document.querySelectorAll("button");

buttons.forEach((item) => {
item.onclick = () => {
if (item.id == "clear") {
display.innerText = "";
} else if (item.id == "backspace") {
let string = display.innerText.toString();
display.innerText = string.substr(0, string.length - 1);
} else if (display.innerText != "" && item.id == "equal") {
display.innerText = eval(display.innerText);
} else if (display.innerText == "" && item.id == "equal") {
display.innerText = "Empty!";
setTimeout(() => (display.innerText = ""), 2000);
} else {
display.innerText += item.id;
item.onclick = () => {
if (item.id == "clear") {
display.innerText = "";
} else if (item.id == "backspace") {
let string = display.innerText.toString();
display.innerText = string.substr(0, string.length - 1);
} else if (display.innerText != "" && item.id == "equal") {
try {
if (display.innerText.includes("/0")) {
throw new Error("Division by zero");
}
};
display.innerText = Function(
'"use strict";return (' + display.innerText + ")"
)();
} catch (e) {
display.innerText = "Cannot divide by zero!";
setTimeout(() => (display.innerText = ""), 2000);
}
} else if (display.innerText == "" && item.id == "equal") {
display.innerText = "Empty!";
setTimeout(() => (display.innerText = ""), 2000);
} else {
const lastChar = display.innerText.slice(-1);
if (
["+", "-", "*", "/"].includes(lastChar) &&
["+", "-", "*", "/"].includes(item.innerText)
) {
// Prevent adding multiple consecutive operators
display.innerText = display.innerText.slice(0, -1) + item.innerText;
} else {
display.innerText += item.innerText;
}
}
};
});

const themeToggleBtn = document.querySelector(".theme-toggler");
const calculator = document.querySelector(".calculator");
const toggleIcon = document.querySelector(".toggler-icon");
let isDark = true;
themeToggleBtn.onclick = () => {
calculator.classList.toggle("dark");
themeToggleBtn.classList.toggle("active");
isDark = !isDark;
};
calculator.classList.toggle("dark");
themeToggleBtn.classList.toggle("active");
isDark = !isDark;
};