-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
35 lines (31 loc) · 1.18 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
document.addEventListener("DOMContentLoaded", function () {
const inputField = document.getElementById("inputField");
const keypadButtons = document.querySelectorAll(".angka, .operator");
const clearButton = document.getElementById("clear");
const backspaceButton = document.getElementById("backspace");
const resultButton = document.getElementById("result");
// Mengatur event listener untuk tombol angka dan operator
keypadButtons.forEach((button) => {
button.addEventListener("click", function () {
const value = button.value;
inputField.value += value;
});
});
// Mengatur event listener untuk tombol AC (Clear)
clearButton.addEventListener("click", function () {
inputField.value = "";
});
// Mengatur event listener untuk tombol CE (Backspace)
backspaceButton.addEventListener("click", function () {
inputField.value = parseFloat(inputField.value);
inputField.value = inputField.value.slice(0, -1);
});
// Mengatur event listener untuk tombol hasil (=)
resultButton.addEventListener("click", function () {
try {
inputField.value = eval(inputField.value);
} catch (error) {
inputField.value = "Error";
}
});
});