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
62 changes: 58 additions & 4 deletions Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
function setAlarm() {}
let countdown;

// DO NOT EDIT BELOW HERE
function getSecondsFromInput() {
const inputValue = document.getElementById("alarmSet").value;
const seconds = Number(inputValue);

// Keep only a safe whole number.
if (!Number.isFinite(seconds) || seconds < 0) {
return 0;
}

return Math.floor(seconds);
}

function setAlarm() {
let seconds = getSecondsFromInput();

function updateDisplay() {
let mins = Math.floor(seconds / 60);
let secs = seconds % 60;
let mm = String(mins).padStart(2, "0");
let ss = String(secs).padStart(2, "0");
document.getElementById("timeRemaining").innerText =
"Time Remaining: " + mm + ":" + ss;
}

// Reset old timer and sound before starting a new one.
pauseAlarm();
updateDisplay();

if (seconds <= 0) {
return;
}

countdown = setInterval(function () {
seconds--;
updateDisplay();

if (seconds <= 0) {
clearInterval(countdown);
playAlarm();
}
}, 1000);
}

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

Expand All @@ -10,7 +51,7 @@ function setup() {
});

document.getElementById("stop").addEventListener("click", () => {
pauseAlarm();
stopAndReset();
});
}

Expand All @@ -19,7 +60,20 @@ function playAlarm() {
}

function pauseAlarm() {
audio.pause();
/* Stop the bell and freeze the timer */
clearInterval(countdown);
try {
audio.pause();
audio.currentTime = 0;
} catch (error) {
// Audio can be missing in tests.
}
}

function stopAndReset() {
pauseAlarm();
document.getElementById("alarmSet").value = "";
document.getElementById("timeRemaining").innerText = "Time Remaining: 00:00";
}

window.onload = setup;
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
43 changes: 43 additions & 0 deletions Sprint-3/alarmclock/style.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
body {
font-family: Arial, sans-serif;
background: #f5f5f5;
margin: 0;
padding: 0;
}

.centre {
position: fixed;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
background: white;
padding: 30px 40px;
border-radius: 12px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
text-align: center;
}

h1 {
margin-bottom: 20px;
font-size: 24px;
color: #333;
}

#alarmSet {
Expand All @@ -12,4 +30,29 @@

h1 {
text-align: center;
border: 2px solid #ddd;
border-radius: 6px;
}

button {
padding: 10px 18px;
margin: 5px;
font-size: 15px;
border: none;
border-radius: 6px;
cursor: pointer;
}

#set {
background: #4caf50;
color: white;
}

#stop {
background: #e53935;
color: white;
}

button:hover {
opacity: 0.85;
}