Skip to content

Commit

Permalink
Update script.js
Browse files Browse the repository at this point in the history
  • Loading branch information
msranjana authored Sep 1, 2024
1 parent ac2a783 commit 69f99f6
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
const display = document.querySelector(".display");
const display = document.querySelector(".display");
const buttons = document.querySelectorAll("button");
const specialChars = ["%", "*", "/", "-", "+", "="];
let output = "";

const calculate = (btnValue) => {
display.focus();
if (btnValue === "=" && output !== "") {

output = eval(output.replace("%", "/100"));
} else if (btnValue === "reset") {
output = "";
} else if (btnValue === "clear") {

output = output.toString().slice(0, -1);
} else {

if (output === "" && specialChars.includes(btnValue)) return;
output += btnValue;
}
display.value = output;
};

buttons.forEach((button) => {

button.addEventListener("click", (e) => calculate(e.target.dataset.value));
});

0 comments on commit 69f99f6

Please sign in to comment.