Skip to content

Commit 33c5b42

Browse files
committed
wrote a function for the alarm
1 parent 4222cda commit 33c5b42

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

‎Sprint-3/alarmclock/alarmclock.js‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1-
function setAlarm() {}
2-
3-
// DO NOT EDIT BELOW HERE
1+
let myInterval;
2+
function setAlarm() {
3+
clearInterval(myInterval);
4+
const userInput = document.querySelector("#alarmSet").value;
5+
let timeRemaining = Number(userInput);
6+
if (isNaN(timeRemaining) || timeRemaining <= 0) {
7+
alert("Please enter a valid number.");
8+
return;
9+
}
10+
myInterval = setInterval(() => {
11+
timeRemaining -= 1;
12+
const minutes = Math.floor(timeRemaining / 60);
13+
const seconds = timeRemaining % 60;
14+
const formattedMinutes = String(minutes).padStart(2, "0");
15+
const formattedSeconds = String(seconds).padStart(2, "0");
16+
const heading = document.querySelector("#timeRemaining");
17+
heading.innerText = `Time Remaining:${formattedMinutes}:${formattedSeconds}`;
18+
if (timeRemaining === 0) {
19+
playAlarm();
20+
clearInterval(myInterval);
21+
}
22+
}, 1000);
23+
}
424

525
var audio = new Audio("alarmsound.mp3");
626

‎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)