Skip to content
Open
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 oneSecondInMilliseconds = 1000;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

These variables are declared but don't appear to be used anywhere. Are they still needed, or can they be removed to keep the code cleaner?

const lengthOfAlarmSound = 20000;

let timeLeft;
let intervalID;
let timeoutID;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is this variable being used anywhere? If not, can it be removed?

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);
if (isNaN(result)) {
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) {
minutes = Math.floor(seconds / 60);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

is minutes supposed to be a global variable?

seconds = seconds % 60;

const seconds_padded = String(seconds).padStart(2, "0");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Does the variable naming convention match style guide?

const minutes_paddded = String(minutes).padStart(2, "0");

const formatted_seconds = `Time Remaining: ${minutes_paddded}:${seconds_padded}`;
return formatted_seconds;
}

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