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
90 changes: 79 additions & 11 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,93 @@
function setAlarm() {}
let timeRemaining = 0;
let timerId = null;

// DO NOT EDIT BELOW HERE
const audio = new Audio("alarmsound.mp3");

var audio = new Audio("alarmsound.mp3");
// FORMAT mm:ss
function formatTime(seconds) {
const mins = Math.floor(seconds / 60);
const secs = seconds % 60;

function setup() {
document.getElementById("set").addEventListener("click", () => {
setAlarm();
});
return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`;
}

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
});
// UPDATE DISPLAY (MUST MATCH TEST EXACTLY)
function updateDisplay() {
const heading = document.getElementById("timeRemaining");
heading.textContent = "Time Remaining: " + formatTime(timeRemaining);
}
Comment on lines +15 to 18
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.

Why not include a parameter to

  • make this function reusable (e.g. call updateDisplay(0) to reset timer display)
  • avoid using a global variable timeRemaining

?


// REQUIRED BY TESTS
function playAlarm() {
audio.loop = true;
audio.currentTime = 0;
audio.play();
}

function pauseAlarm() {
// STOP ALARM
function stopAlarm() {
audio.pause();
audio.currentTime = 0;
audio.loop = false;

clearInterval(timerId);
timerId = null;

document.body.style.backgroundColor = "";
}

// TRIGGER ALARM
function triggerAlarm() {
document.body.style.backgroundColor = "red";
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.

To better separate presentation logic from application logic, you can consider defining a CSS class, and use classList.toggle() to apply/remove the style. For example,

document.body.classList.toggle("alarm-activated", true);  // apply style
document.body.classList.toggle("alarm-activated", false); // remove style

playAlarm();
}

// START TIMER
function startTimer() {
clearInterval(timerId);
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.

Currently when starting a new countdown, the application does not always return to a clean initial state,
which can lead to inconsistent behaviour between runs.

Hint: a user may not click the "Stop" button first before starting a new count down.


timerId = setInterval(() => {
timeRemaining--;

updateDisplay();

if (timeRemaining <= 0) {
clearInterval(timerId);
timerId = null;

timeRemaining = 0;
updateDisplay();

triggerAlarm();
}
}, 1000);
}

// SET ALARM
function setAlarm() {
const input = document.getElementById("alarmSet").value;

// ensure clean number input
timeRemaining = parseInt(input, 10);

if (isNaN(timeRemaining) || timeRemaining < 0) {
timeRemaining = 0;
}

updateDisplay();

if (timeRemaining > 0) {
startTimer();
}
}

// SETUP
function setup() {
document.getElementById("set").addEventListener("click", setAlarm);
document.getElementById("stop").addEventListener("click", stopAlarm);

timeRemaining = 0;
updateDisplay();
}

window.onload = setup;
21 changes: 10 additions & 11 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
<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</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" />
<h1>
Time Remaining: <span id="timeRemaining">00:00</span>
</h1>

<input id="alarmSet" type="number" />
<button id="set">Set Alarm</button>
<button id="stop">Stop Alarm</button>

<button id="set" type="button">Set Alarm</button>
<button id="stop" type="button">Stop Alarm</button>
</div>
<script src="alarmclock.js"></script>
</body>
</html>
</html>
16 changes: 6 additions & 10 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
.centre {
position: fixed;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}

#alarmSet {
margin: 20px;
body {
background-color: blanchedalmond;
}

h1 {
text-align: center;
}

#alarmSet {
margin: 20px;
}
Loading