Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,63 @@
function setAlarm() {}
let timeRemaining;
let timerInterval = null;

// DO NOT EDIT BELOW HERE
function formatTime(time) {
const minutes = String(Math.floor(time / 60)).padStart(2, "0");
const seconds = String(time % 60).padStart(2, "0");

return `${minutes}:${seconds}`;
}

function displayAlarm(time) {
const alarmBox = document.getElementById("timeRemaining");
alarmBox.textContent = `Time Remaining: ${formatTime(time)}`;
}

function decreaseAlarmTime() {
timeRemaining--;
displayAlarm(timeRemaining);

if (timeRemaining <= 0) {
clearInterval(timerInterval);
timerInterval = null;
playAlarm();
return;
}
}

function resetAlarm() {
clearInterval(timerInterval);
timerInterval = null;
pauseAlarm();
audio.currentTime = 0;
}
Comment on lines +16 to +26
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the countdown reaches 00:00, there is a one second delay before the alarm sound is played. Is this by design?

At line 25, when timeRemaining changes from 1 to 0, the app only changes the display to 00:00. It's only in the next interval, the code on lines 17-22 is executed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this definitely a bug -- it wasn't by design.

I've applied a fix by changing the order of execution: timeRemaining is first decremented so that the alarm sound can be played at exactly 00:00 without needing to start a new loop.


function setAlarm() {
const setTime = document.getElementById("alarmSet").value;

const numericTime = parseInt(setTime, 10);
if (isNaN(numericTime)) {
alert(
"Please enter your desired time in numbers, e.g., 120 for 2 minutes."
);
return;
}
if (numericTime < 0) {
alert("Please enter a non-negative number.");
return;
}

resetAlarm();
timeRemaining = numericTime;

displayAlarm(timeRemaining);

if (timeRemaining === 0) {
playAlarm();
} else {
timerInterval = setInterval(decreaseAlarmTime, 1000);
}
}

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

Expand Down
4 changes: 2 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Title here</title>
<title>Alarm clock app</title>
</head>
<body>
<div class="centre">
Expand Down
Loading