-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
100 lines (85 loc) · 2.45 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
const buttons = document.querySelectorAll(".button");
const outputNode = document.querySelector(".output");
const timeNode = document.querySelector(".current-time");
const batteryPercentageNode = document.querySelector(".battery-percentage");
let currentInput = "";
let currentOperation = "";
let previousInput = "";
let result = null;
function updateOutput() {
outputNode.textContent = currentInput;
}
function performOperation() {
const prev = parseFloat(previousInput);
const current = parseFloat(currentInput);
if (isNaN(prev) || isNaN(current)) return;
switch (currentOperation) {
case "+":
currentInput = prev + current;
break;
case "-":
currentInput = prev - current;
break;
case "*":
currentInput = prev * current;
break;
case "/":
currentInput = prev / current;
break;
}
currentOperation = "";
result = currentInput;
currentInput = "";
}
function handleButtonClick(event) {
const value = event.target.textContent;
if (isNaN(parseInt(value)) && value !== ".") {
if (value === "C") {
currentInput = "";
previousInput = "";
currentOperation = "";
result = null;
} else if (value === "+/-") {
currentInput = -parseFloat(currentInput);
} else if (value === "%") {
currentInput = parseFloat(currentInput) / 100;
} else if (value === "=") {
performOperation();
currentOperation = "";
currentInput = result;
} else {
if (currentInput && previousInput && currentOperation) {
performOperation();
}
currentOperation = value;
previousInput = currentInput;
currentInput = "";
}
} else {
currentInput += value;
}
updateOutput();
}
buttons.forEach((button) => {
button.addEventListener("click", handleButtonClick);
});
function showCurrentTimeEuropean() {
let currentTime = new Date();
let hours = currentTime.getHours();
let minutes = currentTime.getMinutes();
minutes = minutes < 10 ? "0" + minutes : minutes;
let timeString = hours + ":" + minutes;
timeNode.innerHTML = timeString;
}
showCurrentTimeEuropean();
function getBatteryPercentage() {
if ("getBattery" in navigator) {
navigator.getBattery().then(function (battery) {
let batteryPercentage = Math.round(battery.level * 100);
batteryPercentageNode.innerHTML = batteryPercentage + "%";
});
} else {
console.log("Battery Status API is not supported.");
}
}
getBatteryPercentage();