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
51 changes: 50 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,53 @@
function setAlarm() {}
let timer = null;

function updateHeading(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");

document.getElementById("timeRemaining").innerText =
`Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
}

function setAlarm() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Have you done appropriate input validation on the input time?

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.

I didn’t add input validation before but I’ve now updated it to validate the input before starting the timer it only allows valid positive numbers

if (timer) {
clearInterval(timer);
}

const input = document.getElementById("alarmSet").value;
let secondsRemaining = Number(input);

if (!Number.isInteger(secondsRemaining) || secondsRemaining <= 0) {
alert("Please enter a valid positive number of seconds.");
return;
}


updateHeading(secondsRemaining);

timer = setInterval(() => {
secondsRemaining--;

if (secondsRemaining >= 0) {
updateHeading(secondsRemaining);
}

if (secondsRemaining === 0) {
playAlarm();
clearInterval(timer);

// Optional extra task - flash the background
let flash = false;

setInterval(() => {
document.body.style.backgroundColor = flash ? "white" : "red";
flash = !flash;
}, 500);
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

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