Skip to content

Commit e30ba62

Browse files
committed
tasks under Sprint 3 alarm has been done
1 parent de6889c commit e30ba62

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

Sprint-3/alarmclock/alarmclock.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,65 @@
11
function setAlarm() {}
2+
const display = document.querySelector("#timeRemaining");
3+
const totalSeconds = document.querySelector("#alarmSet");
4+
const setButton = document.querySelector("#set");
5+
const stopButton = document.querySelector("#stop");
6+
let conuntdownInterval;
7+
8+
// time management function
9+
function formatTime(totalSeconds) {
10+
const minutes = Math.floor(totalSeconds / 60);
11+
const seconds = totalSeconds % 60;
12+
13+
const paddedMinutes = String(minutes).padStart(2, "0");
14+
const paddedSeconds = String(seconds).padStart(2, "0");
15+
return `Time Remaining: ${paddedMinutes}:${paddedSeconds}`;
16+
}
17+
18+
// to clear every change made with conditions
19+
function clearEverything() {
20+
display.style.color = "black";
21+
setButton.disabled = false;
22+
document.body.style.background = "";
23+
}
24+
25+
// button event handler
26+
setButton.addEventListener("click", () => {
27+
let timeleft = Number(totalSeconds.value);
28+
setButton.disabled = true;
29+
30+
// time 0
31+
32+
if (timeleft <= 0) {
33+
alert("please enter a number greater than 0!");
34+
return;
35+
}
36+
37+
display.textContent = formatTime(timeleft);
38+
39+
conuntdownInterval = setInterval(() => {
40+
if (timeleft == 0) {
41+
clearInterval(conuntdownInterval);
42+
document.body.style.backgroundColor = "grey";
43+
playAlarm();
44+
}
45+
46+
if (timeleft <= 10) {
47+
display.style.color = "red";
48+
}
49+
50+
if (timeleft >= 0) {
51+
display.textContent = formatTime(timeleft);
52+
timeleft--;
53+
}
54+
}, 1000);
55+
});
56+
57+
//stop button clicked
58+
stopButton.addEventListener("click", () => {
59+
clearInterval(conuntdownInterval);
60+
clearEverything();
61+
pauseAlarm();
62+
});
263

364
// DO NOT EDIT BELOW HERE
465

Sprint-3/alarmclock/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<link rel="stylesheet" href="style.css" />
7-
<title>Title here</title>
7+
<title>Alarm clock app</title>
88
</head>
99
<body>
1010
<div class="centre">

0 commit comments

Comments
 (0)