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
22 changes: 21 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
function setAlarm() {}
function setAlarm() {
let seconds = parseInt(document.getElementById("alarmSet").value, 10);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What does parseInt("", 10) give you? Try clicking Set Alarm with the box empty and watch the heading and then think about whether seconds <= 0 on line 16 will ever be true.

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

function formatTime(s) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

formatTime gets rebuilt every time setAlarm runs.

const mins = String(Math.floor(s / 60)).padStart(2, "0");
const secs = String(s % 60).padStart(2, "0");
return `Time Remaining: ${mins}:${secs}`;
}

heading.innerText = formatTime(seconds);

const timer = setInterval(() => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Set a 10 second alarm and then few seconds later set a 30 second one without refreshing. Watch the heading closely. What is happening, and what does const timer being declared inside setAlarm have to do with it?

seconds--;
heading.innerText = formatTime(seconds);
if (seconds <= 0) {
clearInterval(timer);
playAlarm();
}
}, 1000);
}

// DO NOT EDIT BELOW HERE

Expand Down
2 changes: 1 addition & 1 deletion Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<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 app</title>
</head>
<body>
<div class="centre">
Expand Down
Loading