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
48 changes: 47 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
function setAlarm() {}
const timeRemaining = document.querySelector("#timeRemaining");

let intervalId; //declared here so that I can use it in line-17, before asigning on line 21.
const alarmSetInput = document.querySelector("#alarmSet");

function setAlarm() {

// I will use the function showTime twice, one for showing the time just after the user clicks the set button and another one for updating the time every second in countdown function
function showTime(inputValue) {
// getting the mins & sec from the inputValue(line19) in appropriate format
const minutes = Math.floor(inputValue / 60)
.toString()
.padStart(2, "0");
const seconds = (inputValue % 60).toString().padStart(2, "0");
//Gathering everything together and outputing remaining time
timeRemaining.innerText = `Time Remaining: ${minutes}:${seconds}`;
}
//the next line in case if we want to reset the time for alarm
clearInterval(intervalId);
// getting user's input value in number format
let inputValue = Number(alarmSetInput.value);
if (inputValue <= 0) {
timeRemaining.innerText = "Enter a positive number.";
alarmSetInput.value = "";
return;
}
showTime(inputValue);
intervalId = setInterval(countdown, 1000);

function countdown() {
if (inputValue > 0) {
inputValue--;
showTime(inputValue);
}
// When the inputValue is 0, play the alarm and clear interval so that the function stops initialising
else {
playAlarm();
clearInterval(intervalId);
alarmSetInput.value = "";
}
}
}
document.getElementById("stop").addEventListener("click", () => {
clearInterval(intervalId);
timeRemaining.innerText = `Time Remaining: 00:00`;
alarmSetInput.value = "";
});

// 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
6 changes: 5 additions & 1 deletion Sprint-3/alarmclock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
"bugs": {
"url": "https://github.com/CodeYourFuture/CYF-Coursework-Template/issues"
},
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme"
"homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme",
"devDependencies": {
"@testing-library/jest-dom": "^6.9.1",
"jest-environment-jsdom": "^30.3.0"
}
}
Loading