Skip to content
Open
Show file tree
Hide file tree
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
62 changes: 61 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
function setAlarm() {}
let timerInterval = null;

// Resets timer state
function resetTimer() {
if (timerInterval !== null) {
clearInterval(timerInterval);
timerInterval = null;
}

// Stop any playing alarm sound
pauseAlarm();

// Reset display to 00:00
updateTimeDisplay(0);
}
Comment thread
cjyuan marked this conversation as resolved.

//Starts the alarm counbtdown
function setAlarm() {
// Read the minutes value from the alarm input field
const minutesInput = document.getElementById("alarmSet");
const seconds = parseInt(minutesInput.value, 10);

// Ignore invalid or non-positive input

if (isNaN(seconds) || seconds <= 0) {
return;
}
// Reset any existing timer first
resetTimer();

//local variable (no global sharing)
let remainingSeconds = seconds;

// Update display immediately
updateTimeDisplay(remainingSeconds);

// Start countdown
timerInterval = setInterval(() => {
remainingSeconds--;

updateTimeDisplay(remainingSeconds);

if (remainingSeconds <= 0) {
resetTimer();
updateTimeDisplay(0);
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.

Line 45 is unnecessary.

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.

Thank you :)

playAlarm();
}
}, 1000);
}

// Converts remainingSeconds into MM:SS format and updates the display
function updateTimeDisplay(remainingSeconds) {
const minutes = Math.floor(remainingSeconds / 60);
Comment thread
cjyuan marked this conversation as resolved.
const seconds = remainingSeconds % 60;

const formattedMinutes = minutes.toString().padStart(2, "0");
const formattedSeconds = seconds.toString().padStart(2, "0");

const timeDisplay = document.getElementById("timeRemaining");
timeDisplay.textContent = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
}

// 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