Skip to content
Closed
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
2 changes: 1 addition & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ const author = {
};

for (const value of author) {
console.log(value);
console.log(value);
}
133 changes: 133 additions & 0 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
function setAlarm() {}

// DO NOT EDIT BELOW HERE
Expand All @@ -23,3 +24,135 @@ function pauseAlarm() {
}

window.onload = setup;
*/


let countdownInterval = null;
let flashInterval = null;
let timeRemaining = 0;
let isPaused = false;

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

const padMinutes = String(minutes).padStart(2, "0");
const padSeconds = String(seconds).padStart(2, "0");

return `${padMinutes}:${padSeconds}`;
}

function updateDisplay() {
const timeRemainingElement = document.getElementById("timeRemaining");
if (timeRemainingElement) {
timeRemainingElement.innerText = `Time Remaining: ${formatTime(timeRemaining)}`;
}
}

function startFlashingBackground() {
let isRed = false;
flashInterval = setInterval(() => {
document.body.style.backgroundColor = isRed ? "#ffffff" : "#ff4d4d";
isRed = !isRed;
}, 500);
}

function stopFlashingBackground() {
if (flashInterval) {
clearInterval(flashInterval);
flashInterval = null;
}
document.body.style.backgroundColor = "";
}

function setAlarm() {
const alarmInput = document.getElementById("alarmSet");

// If currently paused, resume countdown
if (isPaused) {
isPaused = false;
startCountdown();
return;
}

// If an active alarm is already running, toggle pause state
if (countdownInterval !== null) {
isPaused = true;
clearInterval(countdownInterval);
countdownInterval = null;
return;
}

// Read time from input field
const inputSeconds = parseInt(alarmInput.value, 10);
if (isNaN(inputSeconds) || inputSeconds <= 0) {
alert("Please enter a valid number of seconds.");
return;
}

timeRemaining = inputSeconds;
stopFlashingBackground();
updateDisplay();
startCountdown();
}

function startCountdown() {
// Clear any existing timer before starting a new one
if (countdownInterval) {
clearInterval(countdownInterval);
}

countdownInterval = setInterval(() => {
timeRemaining -= 1;
updateDisplay();

if (timeRemaining <= 0) {
clearInterval(countdownInterval);
countdownInterval = null;
playAlarm();
startFlashingBackground();
}
}, 1000);
}

// Override pauseAlarm to also clear screen effects and reset timers
const originalPauseAlarm = pauseAlarm;
pauseAlarm = function () {
if (typeof originalPauseAlarm === "function") {
originalPauseAlarm();
} else {
audio.pause();
}

// Reset state
if (countdownInterval) {
clearInterval(countdownInterval);
countdownInterval = null;
}
isPaused = false;
stopFlashingBackground();
};

// DO NOT EDIT BELOW HERE

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

function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});

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

function playAlarm() {
audio.play();
}

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

window.onload = setup;
2 changes: 1 addition & 1 deletion Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<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