Skip to content
Closed
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
34 changes: 32 additions & 2 deletions Sprint-3/alarmclock/alarmclock.js

Choose a reason for hiding this comment

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

Good job! Your code works and does everything it’s supposed to. In real projects, you might want to:

  1. Make sure the user can’t enter letters or negative numbers.
    2 Stop multiple countdowns if someone clicks the button more than once.
    3 Save the DOM elements in a variable instead of looking them up every second, to make it a bit faster.

But I think for this exercise what you have done is okay,

Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
function setAlarm() {}
function setAlarm() {
// get the value from input and show it in the h1
let input = document.getElementById("alarmSet").value;
let time = Number(input);

// DO NOT EDIT BELOW HERE
document.getElementById(
"timeRemaining"
).textContent = `Time Remaining: 00:${time.toString().padStart(2, "0")}`;
document.title = `Time Remaining: 00:${time.toString().padStart(2, "0")}`;

// start the countdown
const interval = setInterval(() => {
//make sure the input is not null
if (input !== "") {
//decress the numnber
time--;

//update the h1
document.getElementById(
"timeRemaining"
).textContent = `Time Remaining: 00:${time.toString().padStart(2, "0")}`;
document.title = `Time Remaining: 00:${time.toString().padStart(2, "0")}`;

//make sure just countdown to 0
if (time <= 0) {
clearInterval(interval);
playAlarm();
}
}
}, 1000);
}

// DO NOT EDIT BELOW.# HERE

var audio = new Audio("alarmsound.mp3");

Expand Down
37 changes: 20 additions & 17 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<!DOCTYPE html>
<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>
</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>
</div>
<script src="alarmclock.js"></script>
</body>
</html>
<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>
</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>
</div>
<script src="alarmclock.js"></script>
</body>

</html>