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
47 changes: 46 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
function setAlarm() {}
let countdownId;
let secondsRemaining = 0;

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

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

return `${formattedMinutes}:${formattedSeconds}`;
}

function updateTimeRemaining(seconds) {
const heading = document.getElementById("timeRemaining");

heading.textContent = `Time Remaining: ${formatTime(seconds)}`;
}

function setAlarm() {
window.clearInterval(countdownId);
pauseAlarm();
audio.currentTime = 0;

Comment on lines +21 to +24

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.

A user may not click the "Stop" button first before starting a new count down. Are there any other application states that should also be reset?

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. I updated setAlarm() to pause and rewind any existing alarm audio before starting a new countdown.

const input = document.getElementById("alarmSet");
const enteredSeconds = Number.parseInt(input.value, 10);

if (Number.isNaN(enteredSeconds) || enteredSeconds <= 0) {
secondsRemaining = 0;
updateTimeRemaining(secondsRemaining);
return;
}

secondsRemaining = enteredSeconds;
updateTimeRemaining(secondsRemaining);

countdownId = window.setInterval(() => {
secondsRemaining -= 1;
updateTimeRemaining(secondsRemaining);

if (secondsRemaining === 0) {
window.clearInterval(countdownId);
playAlarm();
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
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