-
-
Notifications
You must be signed in to change notification settings - Fork 327
London| 26-ITP-May | Mandip Sanger | Sprint 3 | Alarm Clock #1435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,62 @@ | ||
| function setAlarm() {} | ||
| let timer; | ||
|
|
||
| function setAlarm() { | ||
| const input = document.getElementById("alarmSet"); | ||
| const heading = document.getElementById("timeRemaining"); | ||
| // step 1 retrieve input value | ||
| const inputValue = input.value; | ||
|
|
||
| // step 2 validate if input is a number | ||
| const numberValue = Number(inputValue); | ||
| if (isNaN(numberValue)) { | ||
| alert("only enter a valid number."); | ||
| return; // exit a function if not a number | ||
| } | ||
|
|
||
| //step 3 validate if it is an integer | ||
| if (!Number.isInteger(numberValue)) { | ||
| alert("only enter an integer value."); | ||
| return; // exit a function if not an integer | ||
| } | ||
|
|
||
| //step 4 validate if it is greater than 0 | ||
| if (numberValue <= 0) { | ||
| alert("only enter a number which is bigger than 0"); | ||
| return; //Exit a funciton if not greater than 0 | ||
| } | ||
|
|
||
| let timeRemaining = Number(input.value); | ||
|
|
||
| function formatTime(totalSeconds) { | ||
| const minutes = Math.floor(totalSeconds / 60); | ||
| const seconds = totalSeconds % 60; | ||
|
|
||
| return `Time Remaining: ${String(minutes).padStart(2, "0")}:${String( | ||
| seconds | ||
| ).padStart(2, "0")}`; | ||
| } | ||
|
|
||
| // Show starting time | ||
| heading.innerText = formatTime(timeRemaining); | ||
|
|
||
| // Stop an existing timer | ||
| clearInterval(timer); | ||
|
|
||
|
Comment on lines
+42
to
+44
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note: a user may not click the "Stop Alarm" button first before starting a new count down. |
||
| timer = setInterval(() => { | ||
| timeRemaining--; | ||
|
|
||
| if (timeRemaining <= 0) { | ||
| timeRemaining = 0; | ||
| heading.innerText = formatTime(timeRemaining); | ||
|
|
||
| clearInterval(timer); | ||
| playAlarm(); | ||
| return; | ||
| } | ||
|
|
||
| heading.innerText = formatTime(timeRemaining); | ||
| }, 1000); | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,17 +4,17 @@ | |
| <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</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" /> | ||
| <label for="alarmSet">Set time in seconds:</label> | ||
| <input id="alarmSet" type="number" min="1" step="1" placeholder="Enter seconds" /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: Without a form, the browser won't check the input against the constraints Change is optional. |
||
|
|
||
| <button id="set" type="button">Set Alarm</button> | ||
| <button id="stop" type="button">Stop Alarm</button> | ||
| </div> | ||
| <script src="alarmclock.js"></script> | ||
| </body> | ||
| </html> | ||
| </html> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The integer check could also pick up
NaN.