Skip to content
34 changes: 33 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
function setAlarm() {}
function timeConvert(sec) {
let minutes = String(Math.floor(sec / 60)).padStart(2, "0");
let seconds = String(sec % 60).padStart(2, "0");
return `${minutes}:${seconds}`;
}

let timer;
function setAlarm() {
clearInterval(timer);
pauseAlarm();

const timeInput = document.getElementById("alarmSet");
let remainingSeconds = parseInt(timeInput.value);
if (isNaN(remainingSeconds) || remainingSeconds <= 0) {
alert("Please provide a valid number of seconds");
return;
}

timeInput.value = "";

const timeRemaining = document.getElementById("timeRemaining");

timeRemaining.textContent = `Time Remaining: ${timeConvert(remainingSeconds)}`;

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.

Could also consider turning this statement into a function:

  • Same code is used on line 27
  • displayTime(remainingSeconds) is probably more readable


timer = setInterval(() => {
remainingSeconds--;
timeRemaining.textContent = `Time Remaining: ${timeConvert(remainingSeconds)}`;
if (remainingSeconds === 0) {
playAlarm();
clearInterval(timer);
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

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>Alarm clock app</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>
Loading