From 0cb10c07ba4552fe41016c4d70d635a67c30014b Mon Sep 17 00:00:00 2001 From: Martin Mwaka Date: Fri, 17 Jul 2026 15:04:31 +0100 Subject: [PATCH 1/3] Add title to index.html --- Sprint-3/alarmclock/index.html | 37 ++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/Sprint-3/alarmclock/index.html b/Sprint-3/alarmclock/index.html index 48e2e80d99..36715d7d91 100644 --- a/Sprint-3/alarmclock/index.html +++ b/Sprint-3/alarmclock/index.html @@ -1,20 +1,23 @@ - - - - - Title here - - -
-

Time Remaining: 00:00

- - - - -
- - - + + + + + Alarm clock app + + + +
+

Time Remaining: 00:00

+ + + + + +
+ + + + \ No newline at end of file From c12f5446550a9f1e9409daf3cd9b95aa9615c12b Mon Sep 17 00:00:00 2001 From: Martin Mwaka Date: Fri, 17 Jul 2026 17:42:45 +0100 Subject: [PATCH 2/3] write code to get alarm clock working in alarmclock.js --- Sprint-3/alarmclock/alarmclock.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b1..2d64442485 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,28 @@ -function setAlarm() {} +const heading = document.querySelector("#timeRemaining"); +const input = document.querySelector("#alarmSet"); +const startBtn = document.querySelector("#set"); +const stopBtn = document.querySelector("#stop"); + +function setAlarm() { + let totalSeconds = Number(input.value); + let timer = null; + + timer = setInterval(() => { + const minutes = Math.floor(totalSeconds / 60); + const seconds = totalSeconds % 60; + const mm = String(minutes).padStart(2, "0"); + const ss = String(seconds).padStart(2, "0"); + heading.innerText = `Time Remaining: ${mm}:${ss}`; + if (totalSeconds === 0) { + timer = null; + playAlarm(); + return; + } + totalSeconds--; + }, 1000); + + input.value = ""; +} // DO NOT EDIT BELOW HERE From 32b655e7cbf15ee7b3cd7f0590d1c8be24f33a74 Mon Sep 17 00:00:00 2001 From: Martin Mwaka Date: Sat, 18 Jul 2026 08:11:28 +0100 Subject: [PATCH 3/3] complete alarmclock.js - all tests pass --- Sprint-3/alarmclock/alarmclock.js | 45 ++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 2d64442485..9f8f1606c6 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -3,22 +3,49 @@ const input = document.querySelector("#alarmSet"); const startBtn = document.querySelector("#set"); const stopBtn = document.querySelector("#stop"); +let timer = null; + +function renderTime(totalSeconds) { + const minutes = Math.floor(totalSeconds / 60); + const seconds = totalSeconds % 60; + const mm = String(minutes).padStart(2, "0"); + const ss = String(seconds).padStart(2, "0"); + heading.innerText = `Time Remaining: ${mm}:${ss}`; +} + function setAlarm() { - let totalSeconds = Number(input.value); - let timer = null; + const rawValue = input.value.trim(); + if (rawValue === "") { + return; + } + + let totalSeconds = Number(rawValue); + if (Number.isNaN(totalSeconds) || totalSeconds < 0) { + return; + } + + if (timer !== null) { + clearInterval(timer); + } + + renderTime(totalSeconds); + + if (totalSeconds === 0) { + playAlarm(); + timer = null; + input.value = ""; + return; + } timer = setInterval(() => { - const minutes = Math.floor(totalSeconds / 60); - const seconds = totalSeconds % 60; - const mm = String(minutes).padStart(2, "0"); - const ss = String(seconds).padStart(2, "0"); - heading.innerText = `Time Remaining: ${mm}:${ss}`; + totalSeconds--; + renderTime(totalSeconds); + if (totalSeconds === 0) { + clearInterval(timer); timer = null; playAlarm(); - return; } - totalSeconds--; }, 1000); input.value = "";