-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (45 loc) · 1.48 KB
/
Copy pathscript.js
File metadata and controls
49 lines (45 loc) · 1.48 KB
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
// Function to evaluate a mathematical expression
function calculate(expression) {
try {
// Use the Function constructor to safely evaluate the expression
const result = new Function("return " + expression)();
// Check if the result is a finite number
if (isFinite(result)) {
return result;
} else {
throw new Error("Invalid result");
}
} catch (error) {
throw new Error("Invalid expression");
}
}
// Event listener for button clicks
function handleButtonClick(e) {
const buttonValue = e.target.innerHTML;
// Handle different button clicks
if (buttonValue === "=") {
try {
// Get the input value and calculate the result
const input = document.getElementById("inputBox");
const result = calculate(input.value);
input.value = result;
} catch (error) {
document.getElementById("inputBox").value = "Error";
}
} else if (buttonValue === "AC") {
// Clear the input
document.getElementById("inputBox").value = "";
} else if (buttonValue === "DEL") {
// Delete the last character from the input
const input = document.getElementById("inputBox");
input.value = input.value.slice(0, -1);
} else {
// Append the button value to the input
document.getElementById("inputBox").value += buttonValue;
}
}
// Add event listeners to calculator buttons
const buttons = document.querySelectorAll("button");
buttons.forEach((button) => {
button.addEventListener("click", handleButtonClick);
});