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
38 changes: 36 additions & 2 deletions Sprint-3/alarmclock/alarmclock.js
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  1. playAlarm() no definition
  2. Inconsistent time formats

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

parseInt(input)
if user enter character instead of Int?

Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
function setAlarm() {}
function setAlarm() {
const input = document.getElementById("alarmSet").value;

if (!input || input <= 0) {
alert("Please enter a valid number");
return;
}

let timeRemaining = parseInt(input);
const display = document.getElementById("timeRemaining");

const minutes = Math.floor(timeRemaining / 60);
const seconds = timeRemaining % 60;

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

// Clear any previous timer
clearInterval(countdown);

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

display.textContent = `Time Remaining: 00:${timeRemaining
.toString()
.padStart(2, "0")}`;

if (timeRemaining === 0) {
clearInterval(countdown);
playAlarm();
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

let countdown; // 👈 needed for timer control

var audio = new Audio("alarmsound.mp3");

function setup() {
Expand All @@ -20,6 +53,7 @@ function playAlarm() {

function pauseAlarm() {
audio.pause();
clearInterval(countdown);
}

window.onload = setup;
window.onload = setup;
Loading