Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
71 changes: 67 additions & 4 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,70 @@
function setAlarm() {}

// DO NOT EDIT BELOW HERE
let countdownInterval = null;
let secondsLeft = 0;
const heading = document.getElementById("timeRemaining");

// Move audio to outer scope so stopAndResetAlarm can reach it
var audio = new Audio("alarmsound.mp3");

function stopAndResetAlarm() {
audio.pause();
audio.currentTime = 0;
}

function setAlarm() {
// Stop any currently ringing alarm before starting a new one
stopAndResetAlarm();
stopFlashing(); // reset background too

const input = document.getElementById("alarmSet");
const seconds = parseInt(input.value, 10);
if (isNaN(seconds) || seconds <= 0) {
return;
}

secondsLeft = seconds;

if (countdownInterval !== null) {
clearInterval(countdownInterval);
countdownInterval = null;
}

updateTimeDisplay();

countdownInterval = setInterval(() => {
secondsLeft = secondsLeft - 1;
updateTimeDisplay();
if (secondsLeft <= 0) {
clearInterval(countdownInterval);
countdownInterval = null;
secondsLeft = 0;
updateTimeDisplay();
playAlarm();
startFlashing(); // flash when alarm fires
}
}, 1000);
}
Comment on lines +36 to +45
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can you walk me through what will happen on every "tick" from 2 seconds down to 0? Is there something in this statement we can remove that might be redundant?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

From 2 → 1 nothing happens in this block, and when it reaches 0 the condition becomes true and the interval is cleared, display updated, and alarm played. The assignment secondsLeft = 0 is redundant here because the value is already 0 when the condition is met (assuming it doesn’t go negative), so it can be removed.


function updateTimeDisplay() {
const minutes = Math.floor(secondsLeft / 60);
const seconds = secondsLeft % 60;
const displayMin = minutes.toString().padStart(2, "0");
const displaySec = seconds.toString().padStart(2, "0");
heading.textContent = `Time Remaining: ${displayMin}:${displaySec}`;
}

function startFlashing() {
document.body.classList.add("flashing");
}

function stopFlashing() {
document.body.classList.remove("flashing");
}

// DO NOT EDIT BELOW HERE
function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});
Expand All @@ -20,6 +76,13 @@ function playAlarm() {

function pauseAlarm() {
audio.pause();
stopFlashing(); // reset background

// Also pause the countdown
if (countdownInterval !== null) {
clearInterval(countdownInterval);
countdownInterval = null;
}
}

window.onload = setup;
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
14 changes: 14 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@
h1 {
text-align: center;
}

@keyframes flash {
0%,
100% {
background-color: white;
}
50% {
background-color: red;
}
}

body.flashing {
animation: flash 0.6s infinite;
}
Loading