-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleep.html
92 lines (79 loc) · 2.51 KB
/
sleep.html
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
<!DOCTYPE html>
<html>
<head>
<title>Sleep Time Calculator</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
}
h1 {
color: #333;
}
.container {
margin-top: 50px;
}
#sleep-time, #wake-time {
margin-top: 20px;
font-size: 18px;
padding: 5px 10px;
}
#result {
margin-top: 20px;
font-size: 20px;
font-weight: bold;
}
#calculate-btn {
margin-top: 20px;
font-size: 16px;
padding: 10px 20px;
background-color: #333;
color: #fff;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Sleep Time Calculator</h1>
<div class="container">
<label for="wake-time">Wake Time:</label>
<input type="time" id="wake-time" value="" />
<br>
<button id="calculate-btn">Calculate</button>
<div id="result"></div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
var wakeTimeInput = document.getElementById("wake-time");
var calculateBtn = document.getElementById("calculate-btn");
var resultDiv = document.getElementById("result");
// Set default wake time to 8:00 AM
wakeTimeInput.value = "08:00";
// Calculate sleep duration when wake time is entered
wakeTimeInput.addEventListener("change", function() {
calculateSleepDuration();
});
// Hide button and calculate sleep duration automatically
calculateBtn.style.display = "none";
calculateSleepDuration();
function calculateSleepDuration() {
var wakeTimeString = wakeTimeInput.value;
var wakeTimeParts = wakeTimeString.split(":");
var wakeHour = parseInt(wakeTimeParts[0]);
var wakeMinute = parseInt(wakeTimeParts[1]);
var sleepDuration = calculateSleepDurationFromWakeTime(wakeHour, wakeMinute);
resultDiv.innerHTML = "You can sleep for approximately " + sleepDuration + " hours.";
}
function calculateSleepDurationFromWakeTime(wakeHour, wakeMinute) {
var sleepHour = wakeHour - 8;
if (sleepHour < 0) {
sleepHour = 24 + sleepHour;
}
var sleepMinute = wakeMinute;
return sleepHour + " hours " + sleepMinute + " minutes";
}
});
</script>
</body>
</html>