Skip to content

Commit 75028ec

Browse files
committed
I have completed sprint 3 alarm clock
1 parent 10b3ac9 commit 75028ec

File tree

3 files changed

+95
-32
lines changed

3 files changed

+95
-32
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,93 @@
1-
function setAlarm() {}
1+
let timeRemaining = 0;
2+
let timerId = null;
23

3-
// DO NOT EDIT BELOW HERE
4+
const audio = new Audio("alarmsound.mp3");
45

5-
var audio = new Audio("alarmsound.mp3");
6+
// FORMAT mm:ss
7+
function formatTime(seconds) {
8+
const mins = Math.floor(seconds / 60);
9+
const secs = seconds % 60;
610

7-
function setup() {
8-
document.getElementById("set").addEventListener("click", () => {
9-
setAlarm();
10-
});
11+
return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`;
12+
}
1113

12-
document.getElementById("stop").addEventListener("click", () => {
13-
pauseAlarm();
14-
});
14+
// UPDATE DISPLAY (MUST MATCH TEST EXACTLY)
15+
function updateDisplay() {
16+
const heading = document.getElementById("timeRemaining");
17+
heading.textContent = "Time Remaining: " + formatTime(timeRemaining);
1518
}
1619

20+
// REQUIRED BY TESTS
1721
function playAlarm() {
22+
audio.loop = true;
23+
audio.currentTime = 0;
1824
audio.play();
1925
}
2026

21-
function pauseAlarm() {
27+
// STOP ALARM
28+
function stopAlarm() {
2229
audio.pause();
30+
audio.currentTime = 0;
31+
audio.loop = false;
32+
33+
clearInterval(timerId);
34+
timerId = null;
35+
36+
document.body.style.backgroundColor = "";
37+
}
38+
39+
// TRIGGER ALARM
40+
function triggerAlarm() {
41+
document.body.style.backgroundColor = "red";
42+
playAlarm();
43+
}
44+
45+
// START TIMER
46+
function startTimer() {
47+
clearInterval(timerId);
48+
49+
timerId = setInterval(() => {
50+
timeRemaining--;
51+
52+
updateDisplay();
53+
54+
if (timeRemaining <= 0) {
55+
clearInterval(timerId);
56+
timerId = null;
57+
58+
timeRemaining = 0;
59+
updateDisplay();
60+
61+
triggerAlarm();
62+
}
63+
}, 1000);
64+
}
65+
66+
// SET ALARM
67+
function setAlarm() {
68+
const input = document.getElementById("alarmSet").value;
69+
70+
// ensure clean number input
71+
timeRemaining = parseInt(input, 10);
72+
73+
if (isNaN(timeRemaining) || timeRemaining < 0) {
74+
timeRemaining = 0;
75+
}
76+
77+
updateDisplay();
78+
79+
if (timeRemaining > 0) {
80+
startTimer();
81+
}
82+
}
83+
84+
// SETUP
85+
function setup() {
86+
document.getElementById("set").addEventListener("click", setAlarm);
87+
document.getElementById("stop").addEventListener("click", stopAlarm);
88+
89+
timeRemaining = 0;
90+
updateDisplay();
2391
}
2492

2593
window.onload = setup;

Sprint-3/alarmclock/index.html

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<link rel="stylesheet" href="style.css" />
7-
<title>Title here</title>
5+
<title>Alarm Clock</title>
86
</head>
7+
98
<body>
10-
<div class="centre">
11-
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
12-
<label for="alarmSet">Set time to:</label>
13-
<input id="alarmSet" type="number" />
9+
<h1>
10+
Time Remaining: <span id="timeRemaining">00:00</span>
11+
</h1>
12+
13+
<input id="alarmSet" type="number" />
14+
<button id="set">Set Alarm</button>
15+
<button id="stop">Stop Alarm</button>
1416

15-
<button id="set" type="button">Set Alarm</button>
16-
<button id="stop" type="button">Stop Alarm</button>
17-
</div>
1817
<script src="alarmclock.js"></script>
1918
</body>
20-
</html>
19+
</html>

Sprint-3/alarmclock/style.css

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
.centre {
2-
position: fixed;
3-
top: 50%;
4-
left: 50%;
5-
-webkit-transform: translate(-50%, -50%);
6-
transform: translate(-50%, -50%);
7-
}
8-
9-
#alarmSet {
10-
margin: 20px;
1+
body {
2+
background-color: blanchedalmond;
113
}
124

135
h1 {
146
text-align: center;
157
}
8+
9+
#alarmSet {
10+
margin: 20px;
11+
}

0 commit comments

Comments
 (0)