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
65 changes: 64 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
function setAlarm() {}
let timerInterval = null;
let currentTime = 0;

function setAlarm() {
// Clear any existing timer
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}

// Get the input value
const input = document.getElementById("alarmSet");
let totalSeconds = parseInt(input.value);

// Validate input (if empty or invalid, default to 0)
if (isNaN(totalSeconds)) {
totalSeconds = 0;
}

currentTime = totalSeconds;

// Update the display
updateTimeDisplay(currentTime);

// Start the countdown
timerInterval = setInterval(() => {
if (currentTime > 0) {
currentTime--;
updateTimeDisplay(currentTime);

// Check if timer reached zero
if (currentTime === 0) {
clearInterval(timerInterval);
timerInterval = null;
playAlarm();
// Change background color
document.body.style.backgroundColor = "red";
}
}
}, 1000);
}

function updateTimeDisplay(totalSeconds) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
const formattedTime = `${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
const heading = document.getElementById("timeRemaining");
heading.textContent = `Time Remaining: ${formattedTime}`;
}

// DO NOT EDIT BELOW HERE

Expand All @@ -16,10 +64,25 @@ function setup() {

function playAlarm() {
audio.play();
// Play continuously by looping
audio.loop = true;
}

function pauseAlarm() {
audio.pause();
audio.currentTime = 0;
// Reset background color when alarm is stopped
document.body.style.backgroundColor = "";

// Clear any ongoing timer
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}

// Reset display to 00:00
currentTime = 0;
updateTimeDisplay(0);
}

window.onload = setup;
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
2 changes: 1 addition & 1 deletion Sprint-3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/user-event": "^14.6.1",
"jest": "^30.0.4",
"jest": "^30.3.0",
"jest-environment-jsdom": "^30.0.4"
}
}
Loading