Skip to content

Commit 5d99247

Browse files
author
Pretty Taruvinga
committed
add alarm clock functionality with countdown timer and audio playback
- format time in mm:ss - update display every second - trigger alarm sound at zero - add stop button to pause audio
1 parent d49e3f1 commit 5d99247

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1-
function setAlarm() {
2-
const input = document.getElementById("alarmSet").value;
3-
const heading = document.getElementById("timeRemaining");
1+
let timeLeft = 0;
2+
let timer = null;
43

5-
let totalSeconds = Number(input);
4+
function formatTime(seconds) {
5+
let mins = String(Math.floor(seconds / 60)).padStart(2, "0");
6+
let secs = String(seconds % 60).padStart(2, "0");
7+
return `${mins}:${secs}`;
8+
}
69

7-
function updateDisplay(seconds) {
8-
const min = Math.floor(seconds / 60);
9-
const secs = seconds % 60;
10+
function setAlarm() {
11+
const input = document.getElementById("alarmSet").value;
12+
const display = document.getElementById("timeRemaining");
1013

11-
const formattedMin = String(min).padStart(2, "0");
12-
const formattedSecs = String(secs).padStart(2, "0");
14+
timeLeft = parseInt(input, 10);
1315

14-
heading.innerText = `Time Remaining: ${formattedMin}:${formattedSecs}`;
16+
if (isNaN(timeLeft) || timeLeft <= 0) {
17+
alert("Please enter a valid number of seconds.");
18+
return;
1519
}
16-
updateDisplay(totalSeconds);
1720

18-
const timer = setInterval(() => {
19-
totalSeconds--;
21+
display.textContent = `Time Remaining: ${formatTime(timeLeft)}`;
2022

21-
if (totalSeconds <= 0) {
22-
updateDisplay(0);
23+
if (timer) {
24+
clearInterval(timer);
25+
}
26+
timer = setInterval(() => {
27+
if (timeLeft > 0) {
28+
timeLeft--;
29+
display.textContent = `Time Remaining: ${formatTime(timeLeft)}`;
30+
} else {
2331
clearInterval(timer);
2432
playAlarm();
25-
} else {
26-
updateDisplay(totalSeconds);
2733
}
2834
}, 1000);
2935
}

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">

Sprint-3/quote-generator/quotes.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ function showQuote() {
1111
}
1212

1313
button.addEventListener("click", showQuote);
14-
15-
window.onload = showQuote;
14+
document.addEventListener("DOMContentLoaded", showQuote);
1615

1716
// DO NOT EDIT BELOW HERE
1817

0 commit comments

Comments
 (0)