-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
116 lines (102 loc) · 3.02 KB
/
script.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
const resultElement = document.getElementById("result");
const clearBtn = document.getElementById("clear-button");
const deleteBtn = document.getElementById("delete-button");
const divideBtn = document.getElementById("divide-button");
const multiplyBtn = document.getElementById("multiply-button");
const subtractBtn = document.getElementById("subtract-button");
const addBtn = document.getElementById("add-button");
const decimalBtn = document.getElementById("decimal-button");
const equalBtn = document.getElementById("equal-button");
const number = document.querySelectorAll(".number");
// Initialize variables
let result = "";
let operation = "";
let previousOperand = null;
// Append number function
const appendNumber = (number) => {
if (number === "." && result.includes(".")) return;
result += number;
updateDisplay();
};
// Function to update display
const updateDisplay = () => {
if (operation) {
resultElement.innerText = `${previousOperand} ${operation} ${result}`;
} else {
resultElement.innerText = result;
}
};
// Select operator function
const selectOperator = (operateValue) => {
if (result === "") return;
if (operation !== "" && previousOperand !== null) {
calculateResult();
}
operation = operateValue;
previousOperand = parseFloat(result);
result = "";
updateDisplay();
};
// Calculate result function
const calculateResult = () => {
let evaluatedResult;
const prev = previousOperand;
const current = parseFloat(result);
if (isNaN(prev) || isNaN(current)) return;
switch (operation) {
case "+":
evaluatedResult = prev + current;
break;
case "-":
evaluatedResult = prev - current;
break;
case "*":
evaluatedResult = prev * current;
break;
case "/":
if (current === 0) {
evaluatedResult = "Error"; // Handle division by zero
} else {
evaluatedResult = prev / current;
}
break;
default:
return;
}
result = evaluatedResult.toString();
operation = ""; // Reset operation after calculation
previousOperand = null; // Reset previous operand
};
// Add event listeners
number.forEach((button) => {
button.addEventListener("click", () => {
appendNumber(button.innerText);
});
});
decimalBtn.addEventListener("click", () => appendNumber("."));
addBtn.addEventListener("click", () => selectOperator("+"));
subtractBtn.addEventListener("click", () => selectOperator("-"));
multiplyBtn.addEventListener("click", () => selectOperator("*"));
divideBtn.addEventListener("click", () => selectOperator("/"));
equalBtn.addEventListener("click", () => {
if (result === "") return;
calculateResult();
updateDisplay();
});
clearBtn.addEventListener("click", () => {
result = "";
operation = "";
previousOperand = null;
updateDisplay();
});
deleteBtn.addEventListener("click", () => {
if (operation !== "" && result === "") {
operation = "";
result = previousOperand;
previousOperand = null;
updateDisplay();
} else {
result = result.slice(0, -1);
updateDisplay();
}
});