Skip to content

Commit 9027349

Browse files
author
Enice-Codes
committed
added negatice intervals and positve intervals to ensure the alarm only works with positive intervals
1 parent 76cb8e7 commit 9027349

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
let timer = null;
2+
let secondsRemaining = 0;
23

34
function formatTime(totalSeconds) {
45
const minutes = Math.floor(totalSeconds / 60);
@@ -14,22 +15,49 @@ function updateDisplay(seconds) {
1415
function setAlarm() {
1516
if (timer) {
1617
clearInterval(timer);
18+
timer = null;
1719
}
1820

19-
let seconds = Number(document.getElementById("alarmSet").value);
20-
updateDisplay(seconds);
21+
const inputValue = document.getElementById("alarmSet").value;
22+
const seconds = Number(inputValue);
23+
24+
if (
25+
inputValue === "" ||
26+
Number.isNaN(seconds) ||
27+
!Number.isInteger(seconds) ||
28+
seconds <= 0
29+
) {
30+
document.getElementById("timeRemaining").textContent =
31+
"Please enter a whole number greater than 0.";
32+
return;
33+
}
34+
35+
secondsRemaining = seconds;
36+
updateDisplay(secondsRemaining);
2137

2238
timer = setInterval(() => {
23-
seconds--;
24-
updateDisplay(seconds);
39+
secondsRemaining--;
40+
updateDisplay(secondsRemaining);
2541

26-
if (seconds <= 0) {
42+
if (secondsRemaining <= 0) {
2743
clearInterval(timer);
44+
timer = null;
2845
playAlarm();
2946
}
3047
}, 1000);
3148
}
3249

50+
function stopTimer() {
51+
if (timer) {
52+
clearInterval(timer);
53+
timer = null;
54+
}
55+
}
56+
57+
document.addEventListener("DOMContentLoaded", () => {
58+
document.getElementById("stop").addEventListener("click", stopTimer);
59+
});
60+
3361
// DO NOT EDIT BELOW HERE
3462
var audio = new Audio("alarmsound.mp3");
3563

‎Sprint-3/alarmclock/alarmclock.test.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
There are some Tests in this file that will help you work out if your code is working.
33
*/
44

5+
56
const path = require("path");
67
const { JSDOM } = require("jsdom");
78

@@ -15,6 +16,8 @@ beforeEach(async () => {
1516

1617
jest.useFakeTimers();
1718

19+
20+
1821
// do this so students can use element.innerText which jsdom does not implement
1922
Object.defineProperty(page.window.HTMLElement.prototype, "innerText", {
2023
get() {

0 commit comments

Comments
 (0)