Skip to content
Open
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: 70 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,71 @@
function setAlarm() {}
let timerInterval;
let flashInterval;

function setAlarm() {
const inputElement = document.getElementById("alarmSet");
const headingElement = document.getElementById("timeRemaining");

if (!inputElement.value) {
return;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's important to validate user input.

What happens if a user enters a negative value?
How could you give feedback to the user about the input not being valid?

}

if (timerInterval) {
clearInterval(timerInterval);
}
if (flashInterval) {
clearInterval(flashInterval);
}

let timeInSeconds = Number(inputElement.value);

function updateScreen(secondsRemaining) {
const minutes = Math.floor(secondsRemaining / 60);

const seconds = secondsRemaining % 60;

const formattedMinutes = String(minutes).padStart(2, "0");
const formattedSeconds = String(seconds).padStart(2, "0");

headingElement.innerText = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
}

// update when we click set alarm
updateScreen(timeInSeconds);

timerInterval = setInterval(() => {
// Subtract 1 from the time
timeInSeconds = timeInSeconds - 1;

// Update the screen with the new time
updateScreen(timeInSeconds);

// when we hit zero?
if (timeInSeconds <= 0) {
clearInterval(timerInterval);
playAlarm();
flashScreen();
}
}, 1000);
}

function flashScreen() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice bonus implementation of the flashing screen

let flashCount = 0;

flashInterval = setInterval(() => {
flashCount++;

if (document.body.style.backgroundColor === "red") {
document.body.style.backgroundColor = "white";
} else {
document.body.style.backgroundColor = "red";
}

if (flashCount >= 20) {
clearInterval(flashInterval);
document.body.style.backgroundColor = "white";
}
}, 500);
}

// DO NOT EDIT BELOW HERE

Expand All @@ -20,6 +87,8 @@ function playAlarm() {

function pauseAlarm() {
audio.pause();
clearInterval(flashInterval);
document.body.style.backgroundColor = "white";
}

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
6 changes: 5 additions & 1 deletion Sprint-3/alarmclock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
"bugs": {
"url": "https://github.com/CodeYourFuture/CYF-Coursework-Template/issues"
},
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme"
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"devDependencies": {
"@testing-library/jest-dom": "^6.9.1",
"jest-environment-jsdom": "^30.3.0"
}
}
Loading