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,73 @@
function setAlarm() {}
const lengthOfAlarmSound = 20000;

let timeLeft;
let intervalID;
let timeoutID;
let display;

function setAlarm() {
stopAlarm();

// set up time
timeLeft = document.getElementById("alarmSet").value;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What would happen if I set the alarm for -10 seconds?

display = document.getElementById("timeRemaining");

// validate result
const result = parseInt(timeLeft);
console.log(result);
if (isNaN(result) || result < 0 || !Number.isInteger(result)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The integer validation doesn't seem to be working as expected. What value or type is being passed to Number.isInteger()?

result stores the validated input, but is it being used anywhere after the validation?

return;
}

display.textContent = formatTime(timeLeft);

if (!intervalID) {
intervalID = setInterval(updateTimer, 1000);
}

console.log(`Alarm set for ${timeLeft} seconds`);
}

function updateTimer() {
timeLeft = timeLeft - 1;

display.textContent = formatTime(timeLeft);

if (timeLeft <= 0) {
alarmFinished();
cleanUp();
}
}

// give a time in seconds
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
seconds = seconds % 60;

const secondsPadded = String(seconds).padStart(2, "0");
const minutesPaddded = String(minutes).padStart(2, "0");

const formattedPeconds = `Time Remaining: ${minutesPaddded}:${secondsPadded}`;
return formattedPeconds;
}

function stopAlarm() {
pauseAlarm();
cleanUp();
}

function cleanUp() {
clearInterval(intervalID);
clearTimeout(timeoutID);
timeoutID = null;
intervalID = null;
}

function alarmFinished() {
audio.loop = true;
playAlarm();
console.log("Alarm finished");
}

// 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