Skip to content

Commit 99a1af9

Browse files
committed
I created, with the help of google and other ai tools, two functions. the firat sets the alaram. the second update time display
1 parent 57d32a3 commit 99a1af9

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,68 @@
1-
function setAlarm() {}
1+
// This is our main function that gets called when "Set Alarm" button is clicked
2+
function setAlarm() {
3+
// STEP 1: Get the number the user typed in the input box
4+
// document.getElementById finds the HTML element with id="alarmSet" (our input field)
5+
// .value gets whatever number the user typed in that input field
6+
const alarmSet = document.getElementById("alarmSet");
7+
let timeInSeconds = parseInt(alarmSet.value);
8+
9+
// STEP 2: Check if the user entered a valid number
10+
// isNaN means "Is Not a Number" - it checks if what they typed isn't a real number
11+
// We also check if the number is 0 or negative, which wouldn't make sense for a timer
12+
if (isNaN(timeInSeconds) || timeInSeconds <= 0) {
13+
alert("Please enter a valid positive number");
14+
return; // This stops the function here if the input is invalid
15+
}
16+
17+
// STEP 3: Stop any existing countdown (in case user clicks "Set Alarm" multiple times)
18+
// We store our timer in window.countdownInterval so we can access it later
19+
if (window.countdownInterval) {
20+
clearInterval(window.countdownInterval); // This stops the previous timer
21+
}
22+
23+
// STEP 4: Show the initial time immediately (don't wait 1 second for first update)
24+
updateTimeDisplay(timeInSeconds);
25+
26+
// STEP 5: Start the countdown timer
27+
// setInterval runs code repeatedly every X milliseconds (1000ms = 1 second)
28+
window.countdownInterval = setInterval(() => {
29+
// This code runs every second:
30+
31+
// STEP 5a: Decrease time by 1 second
32+
timeInSeconds--;
33+
34+
// STEP 5b: Update the display with new time
35+
updateTimeDisplay(timeInSeconds);
36+
37+
// STEP 5c: Check if time is up
38+
if (timeInSeconds <= 0) {
39+
clearInterval(window.countdownInterval); // Stop the timer
40+
playAlarm(); // Play the alarm sound (this function is provided for us)
41+
}
42+
}, 1000); // 1000 milliseconds = 1 second
43+
}
44+
45+
// This helper function takes seconds and displays them as MM:SS format
46+
function updateTimeDisplay(seconds) {
47+
// STEP 1: Find the HTML element where we show the time
48+
// This finds the <h1> tag with id="timeRemaining" where we display "Time Remaining: 00:00"
49+
const timeRemaining = document.getElementById("timeRemaining");
50+
51+
// STEP 2: Convert total seconds into minutes and seconds
52+
// Math.floor rounds DOWN to nearest whole number (so 65 seconds becomes 1 minute, not 1.08)
53+
const minutes = Math.floor(seconds / 60);
54+
const remainingSeconds = seconds % 60; // % gives us the remainder (65 % 60 = 5 seconds)
55+
56+
// STEP 3: Format numbers to always show 2 digits with leading zeros
57+
// padStart(2, '0') means "make this string 2 characters long by adding '0' to the front if needed"
58+
// So 5 becomes "05", but 12 stays "12"
59+
const formattedMinutes = minutes.toString().padStart(2, "0");
60+
const formattedSeconds = remainingSeconds.toString().padStart(2, "0");
61+
62+
// STEP 4: Update the actual text on the webpage
63+
// This changes what the user sees on screen
64+
timeRemaining.textContent = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`;
65+
}
266

367
// DO NOT EDIT BELOW HERE
468

0 commit comments

Comments
 (0)