Skip to content
50 changes: 49 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
function setAlarm() {}
//global variables: 1- userInput, 2- timeRemaining, 3- stop, 4- seconds, 5- minutes
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please take a look at all of your comments in this file and see if they add more detail than just the line of code itself. e.g. the comment get access to the "timeRemaining" element simply restates exactly what the line after does. Remove any that aren't useful.

Comments should ideally be replaced by clear naming with code, and where they can't, should typically explain why we're doing something, rather than what we're doing. The code itself says what we're doing.


// get access to the "input" element
const userInput = document.querySelector("#alarmSet");
// get access to the "timeRemaining" element
const timeRemaining = document.querySelector("#timeRemaining");
// get access to the "stop" button
const stop = document.querySelector("#stop");
let totalSeconds = 0;

function main() {
// add an event listener to the "input" element that will call the counterUpdate function when clicked on
document.getElementById("alarmSet").addEventListener("click", counterUpdate);
document.getElementById("alarmSet").addEventListener("keyup", counterUpdate);
}

// define the function that will update the counter
function counterUpdate() {
// local variables: useInputValue
// get the value of the input element.
const userInputValue = userInput.value;
// check if the input is equal to or greater than 0
if (userInputValue >= 0) {
totalSeconds = userInputValue;
displyUpdate();
}
}

// Update the TimeRemaining element to show the real time remaining in minutes and seconds.
function displyUpdate() {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
timeRemaining.innerText = `Time Remaining: ${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
}

// define the function that will count down the time remaining every second and update the remaining time on the page
function setAlarm() {
// use setInterval to call a function every second that will decrease the time remaining by 1 second.
const interval = setInterval(() => {
if (totalSeconds > 0) {
totalSeconds--;
displyUpdate();
} else {
clearInterval(interval);
userInput.value = "";
playAlarm();
}
}, 1000);
}
main();
// DO NOT EDIT BELOW HERE

var audio = new Audio("alarmsound.mp3");
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