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
101 changes: 90 additions & 11 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,104 @@
function setAlarm() {}
let timer;
let timeLeft = 0;
let paused = false;
const pauseBtn = document.getElementById("pauseAndResume");
const setBtn = document.getElementById("set");
const inputField = document.getElementById("alarmSet");
const audio = new Audio("alarmsound.mp3");

// DO NOT EDIT BELOW HERE
// update alarm
function updateDisplay() {
const timeDisplay = document.getElementById("timeRemaining");
const mm = String(Math.floor(timeLeft / 60)).padStart(2, "0");
const ss = String(timeLeft % 60).padStart(2, "0");
timeDisplay.textContent = `Time Remaining: ${mm}:${ss}`;
}

// stop or start flashing
let flashingInterval;
const flashingColors = ["#FF4136", "#2ECC40", "#0074D9", "#FFDC00"];

Choose a reason for hiding this comment

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

It's better to keep variable local if it's not used elsewhere. Makes your code cleaner and reduces possibility of bugs due to accidental change to your variable.

function startFlashingBackground() {
let colorIndex = 0;
flashingInterval = setInterval(() => {
document.body.style.backgroundColor = flashingColors[colorIndex];
colorIndex = (colorIndex + 1) % flashingColors.length;
}, 200);
}
function stopFlashingBackground() {
clearInterval(flashingInterval);
document.body.style.backgroundColor = "";
}

var audio = new Audio("alarmsound.mp3");
// start countDown
function startCountdown() {
clearInterval(timer);
timer = setInterval(() => {
if (--timeLeft <= 0) {
clearInterval(timer);
startFlashingBackground();
playAlarm();
pauseBtn.style.display = "none";
updateDisplay();
} else {
updateDisplay();
}
}, 1000);
}

function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});
// set up alarm
function setAlarm() {
const val = parseInt(inputField.value);

Choose a reason for hiding this comment

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

What if inputField.value is empty? Can you try starting the alarm without entering anything into input field?

Copy link
Author

Choose a reason for hiding this comment

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

When inputField.value is empty, the parseInt("") returns NaN,
So I have validated the condition if the value is NaN, Undefined , null or less than 0 , i used return it will stop any further execution so no nothing will happen. I can throw an error message instead.

Copy link

@jennethydyrova jennethydyrova Oct 29, 2025

Choose a reason for hiding this comment

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

You mean you already have it in your code or you are going to push? I don't see validation against NaN, etc.

Copy link
Author

Choose a reason for hiding this comment

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

i have already used it thing i did not push properly

Choose a reason for hiding this comment

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

Ah okay, that happens 🙂

if (!val || val <= 0) {
alert("Please enter a valid number of seconds.");
return;
}

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

setBtn.style.display = "none";
pauseBtn.style.display = "inline";
inputField.style.display = "none";
}

function playAlarm() {
audio.currentTime = 0;
audio.play();
}
// pause or resume alarm
function pauseResumeHandler() {
if (!paused) {
clearInterval(timer);
pauseBtn.textContent = "Resume";
} else {
startCountdown();
pauseBtn.textContent = "Pause";
}
paused = !paused;
}

function pauseAlarm() {
// stop alarm
function stopAlarm() {
clearInterval(timer);
audio.pause();
audio.currentTime = 0;
stopFlashingBackground();
timeLeft = 0;
updateDisplay();

setBtn.style.display = "inline";
pauseBtn.style.display = "none";
inputField.style.display = "inline";
pauseBtn.textContent = "Pause";
paused = false;
}

function setup() {
const stopBtn = document.getElementById("stop");
setBtn.addEventListener("click", setAlarm);
pauseBtn.addEventListener("click", pauseResumeHandler);
stopBtn.addEventListener("click", stopAlarm);
}

window.onload = setup;
15 changes: 8 additions & 7 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
<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">
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
<label for="alarmSet">Set time to:</label>
<input id="alarmSet" type="number" />

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
<h2 id="timeRemaining">Time Remaining: 00:00</h2>
<div>
<input id="alarmSet" type="number" placeholder="Enter seconds" />
<button id="set">Set</button>
<button id="pauseAndResume" style="display: none">Pause</button>
<button id="stop">Stop</button>
</div>
</div>
<script src="alarmclock.js"></script>
</body>
Expand Down
16 changes: 11 additions & 5 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
position: fixed;
top: 50%;
left: 50%;
margin: auto;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
box-shadow: 0px 0px 2px 3px rgba(0, 0, 0, 0.08);
background: #fcfcfc;
padding: 30px;
width: 20%;
}

#alarmSet {
margin: 20px;
}

h1 {
text-align: center;
font-size: 1.3rem;
}
input,
button {
font-size: 1.1rem;
}