Skip to content

Commit 7a7eb43

Browse files
committed
refactor: remove unused variables and fix playAlarm timing
1 parent c4c9260 commit 7a7eb43

1 file changed

Lines changed: 23 additions & 26 deletions

File tree

Sprint-3/alarmclock/alarmclock.js

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1-
function setAlarm() {}
2-
3-
// DO NOT EDIT BELOW HERE
4-
5-
var audio = new Audio("alarmsound.mp3");
6-
7-
function setup() {
8-
document.getElementById("set").addEventListener("click", () => {
9-
setAlarm();
10-
});
11-
12-
document.getElementById("stop").addEventListener("click", () => {
13-
pauseAlarm();
14-
});
15-
}
16-
171
const state = {
18-
initialTime: 0,
192
remainingTime: 0,
203
timerId: null,
214
};
225

236
function setAlarm() {
7+
if (state.timerId !== null) {
8+
clearInterval(state.timerId);
9+
state.timerId = null;
10+
}
2411
const timeInput = document.getElementById("alarmSet");
25-
state.initialTime = +timeInput.value;
26-
state.remainingTime = state.initialTime;
27-
const formattedTime = formatTime(state.initialTime);
12+
state.remainingTime = +timeInput.value;
13+
const formattedTime = formatTime(state.remainingTime);
2814
timeInput.value = "";
2915

3016
const displayedTime = document.getElementById("timeRemaining");
@@ -45,20 +31,31 @@ function formatTime(seconds) {
4531

4632
function timer() {
4733
const displayedTime = document.getElementById("timeRemaining");
48-
49-
console.log(state.remainingTime);
34+
state.remainingTime -= 1;
35+
const countingDownTime = formatTime(state.remainingTime);
36+
displayedTime.textContent = `Time Remaining: ${countingDownTime}`;
5037
if (state.remainingTime === 0) {
5138
clearInterval(state.timerId);
5239
state.timerId = null;
5340
document.body.style.backgroundColor = "red";
5441
playAlarm();
55-
} else {
56-
state.remainingTime -= 1;
57-
const countingDownTime = formatTime(state.remainingTime);
58-
displayedTime.textContent = `Time Remaining: ${countingDownTime}`;
5942
}
6043
}
6144

45+
// DO NOT EDIT BELOW HERE
46+
47+
var audio = new Audio("alarmsound.mp3");
48+
49+
function setup() {
50+
document.getElementById("set").addEventListener("click", () => {
51+
setAlarm();
52+
});
53+
54+
document.getElementById("stop").addEventListener("click", () => {
55+
pauseAlarm();
56+
});
57+
}
58+
6259
function playAlarm() {
6360
audio.play();
6461
}

0 commit comments

Comments
 (0)