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

function setAlarm() {
const input = document.getElementById("alarmSet");
const heading = document.getElementById("timeRemaining");

let timeRemaining = Number(input.value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Using raw input without validation is a dangerous practice. Is there any value this app should reject?

  • Is the input a number?
  • What type of number should it be?
  • What is the acceptable range of numbers?


function formatTime(totalSeconds) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;

return `Time Remaining: ${String(minutes).padStart(2, "0")}:${String(
seconds
).padStart(2, "0")}`;
}

// Show starting time
heading.innerText = formatTime(timeRemaining);

// Stop an existing timer
clearInterval(timer);

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

if (timeRemaining <= 0) {
timeRemaining = 0;
heading.innerText = formatTime(timeRemaining);

clearInterval(timer);
playAlarm();
return;
}

heading.innerText = formatTime(timeRemaining);
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
Loading