Skip to content

Commit eb5bdbd

Browse files
committed
Added alarmclock functionality
1 parent cfd674c commit eb5bdbd

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
1-
function setAlarm() {}
1+
const alarmInput = document.getElementById("alarmSet");
2+
const timeRemaining = document.getElementById("timeRemaining");
3+
const setButton = document.getElementById("set");
4+
const stopButton = document.getElementById("stop");
5+
6+
let counter;
7+
8+
setButton.addEventListener("click", setAlarm);
9+
stopButton.addEventListener("click", pauseAlarm);
10+
11+
function setText() {
12+
const alarmValue = parseInt(alarmInput.value) || 0;
13+
14+
const minutes = Math.floor(alarmValue / 60);
15+
const seconds = alarmValue % 60;
16+
17+
const displayM = String(minutes).padStart(2, "0");
18+
const displayS = String(seconds).padStart(2, "0");
19+
20+
timeRemaining.textContent = `Time Remaining: ${displayM}:${displayS}`;
21+
}
22+
23+
function countdown() {
24+
let alarmValue = parseInt(alarmInput.value) || 0;
25+
26+
if (alarmValue <= 0) {
27+
playAlarm();
28+
clearInterval(counter);
29+
return;
30+
}
31+
32+
alarmInput.value = alarmValue - 1;
33+
setText();
34+
}
35+
36+
function setAlarm() {
37+
clearInterval(counter);
38+
39+
setText();
40+
41+
counter = setInterval(countdown, 1000);
42+
}
243

344
// DO NOT EDIT BELOW HERE
445

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)