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
56 changes: 39 additions & 17 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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the feedback!

I’ve made the following updates:

  • Added a definition for playAlarm() to resolve the missing function issue.
  • Standardised the time display format to mm:ss for consistency throughout.
  • Replaced parseInt() with Number() and added validation to handle invalid user input (e.g. non-numeric values).
  • Improved overall structure to make the timer logic clearer and more maintainable.

Let me know if there’s anything else I can improve.

Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
function setAlarm() {}
let countdown;

// DO NOT EDIT BELOW HERE
function startTimer(input) {
const display = document.getElementById("timeRemaining");

var audio = new Audio("alarmsound.mp3");
// Validate input
const timeRemaining = Number(input);

function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});
if (isNaN(timeRemaining) || timeRemaining <= 0) {
display.textContent = "Please enter a valid number";
return;
}

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});
}
let remaining = timeRemaining;

function playAlarm() {
audio.play();
}
// Clear any previous timer
clearInterval(countdown);

function updateDisplay() {
const minutes = Math.floor(remaining / 60);
const seconds = remaining % 60;

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

function pauseAlarm() {
audio.pause();
// Show initial time
updateDisplay();

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

updateDisplay();

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

window.onload = setup;
// :white_check_mark: Fix missing function
function playAlarm() {
alert("Time's up!");
}