Skip to content

Commit e4ab2e0

Browse files
committed
Completed the tasks in file alarmclock.js , created 3 different functions in order to complete the task
1 parent e02f0fe commit e4ab2e0

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

Week-3/Homework/mandatory/1-alarmclock/alarmclock.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,59 @@
1-
function setAlarm() {}
1+
function setAlarm() {
2+
3+
let setAlarmbutton=document.querySelector("#set");
4+
let alarmInputValue=document.querySelector("#alarmSet").value;// this is value in box
5+
setAlarmbutton.addEventListener("click",alarmOn(alarmInputValue));
6+
}
7+
8+
function alarmOn(inputValue)
9+
{
10+
let time=setInterval(function()
11+
{
12+
if(inputValue===0)
13+
{
14+
clearInterval(time);
15+
playAlarm();
16+
}
17+
updateScreen(inputValue);
18+
inputValue--;
19+
},1000)
20+
}
21+
22+
function updateScreen(newValue){
23+
let timeRemaining =document.querySelector("#timeRemaining"); // here we are taking the tag by id
24+
textTime =timeRemaining.textContent; // refering to the tags textcontent so that we can update later.
25+
let replacementTime = convertTime(newValue); // this is the variable which will contain the final value in minutes and seconds.
26+
textTime = textTime.slice(0,15) + replacementTime; // updating the string to put it as textcontent on the page.
27+
28+
timeRemaining.textContent =textTime;
29+
}
30+
31+
32+
// This will convert input time (initially in seconds) into minutes and seconds format.
33+
function convertTime(inputValue)
34+
{
35+
var minutes=0;
36+
var seconds =0;
37+
38+
if(inputValue<=60){
39+
minutes=0;
40+
seconds=inputValue;
41+
}
42+
else{
43+
minutes=Math.floor(inputValue/60);
44+
seconds=inputValue-Math.floor(minutes*60);
45+
}
46+
47+
if(minutes<10){
48+
minutes="0"+minutes;
49+
}
50+
if(seconds<10){
51+
seconds="0"+seconds;
52+
}
53+
54+
var finalValue = minutes+":"+seconds;
55+
return finalValue;
56+
}
257

358
// DO NOT EDIT BELOW HERE
459

0 commit comments

Comments
 (0)