-
-
Notifications
You must be signed in to change notification settings - Fork 157
ITP JAN 2025/ELFREDAH KEVIN-ALERECHI/DAT GROUP/ALARM CLOCK #494
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
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,20 +1,97 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<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> | ||
</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" /> | ||
|
||
<button id="set" type="button">Set Alarm</button> | ||
<button id="stop" type="button">Stop Alarm</button> | ||
</div> | ||
<script src="alarmclock.js"></script> | ||
</body> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Alarm Timer</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
text-align: center; | ||
padding: 20px; | ||
} | ||
#alarm-container { | ||
margin-top: 50px; | ||
} | ||
#time-remaining { | ||
font-size: 48px; | ||
margin-bottom: 20px; | ||
font-weight: bold; | ||
} | ||
#alarm-sound { | ||
display: none; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<div id="time-remaining">00:00</div> <!-- Timer moved to the top --> | ||
<div id="alarm-container"> | ||
<h1>Set Alarm</h1> | ||
<input type="text" id="alarm-time" placeholder="Enter time in seconds"> | ||
<button onclick="setAlarm()">Set Alarm</button> | ||
<button id="stop-alarm" onclick="stopAlarm()" style="display: none;">Stop Alarm</button> | ||
<audio id="alarm-sound" src="https://www.soundjay.com/button/beep-07.wav" loop></audio> | ||
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. This audio cannot be loaded. Why not use the one in the project folder? |
||
</div> | ||
|
||
<script> | ||
let timer; | ||
let timeRemaining; | ||
let alarmSound = document.getElementById('alarm-sound'); | ||
let stopButton = document.getElementById('stop-alarm'); | ||
let timeRemainingTitle = document.getElementById('time-remaining'); | ||
|
||
// Function to format time in mm:ss | ||
function formatTime(seconds) { | ||
let minutes = Math.floor(seconds / 60); | ||
let remainingSeconds = seconds % 60; | ||
return `${minutes < 10 ? '0' + minutes : minutes}:${remainingSeconds < 10 ? '0' + remainingSeconds : remainingSeconds}`; | ||
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. Could consider using |
||
} | ||
|
||
// Function to set the alarm | ||
function setAlarm() { | ||
const alarmTime = parseInt(document.getElementById('alarm-time').value); | ||
if (isNaN(alarmTime) || alarmTime <= 0) return; // Validate input | ||
|
||
timeRemaining = alarmTime; | ||
timeRemainingTitle.innerText = formatTime(timeRemaining); | ||
stopButton.style.display = 'inline'; | ||
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. You can also consider using |
||
|
||
// Start countdown | ||
if (timer) clearInterval(timer); // Clear any previous timers | ||
timer = setInterval(() => { | ||
timeRemaining--; | ||
timeRemainingTitle.innerText = formatTime(timeRemaining); | ||
if (timeRemaining <= 0) { | ||
clearInterval(timer); | ||
playAlarm(); | ||
} | ||
}, 1000); | ||
} | ||
|
||
// Function to play the alarm sound | ||
function playAlarm() { | ||
alarmSound.play(); // Play sound | ||
document.body.style.backgroundColor = '#ffcccc'; // Change background color | ||
stopButton.style.display = 'inline'; // Show stop button | ||
} | ||
|
||
// Function to stop the alarm sound | ||
function stopAlarm() { | ||
alarmSound.pause(); | ||
alarmSound.currentTime = 0; | ||
document.body.style.backgroundColor = ''; // Reset background color | ||
stopButton.style.display = 'none'; // Hide stop button | ||
} | ||
Comment on lines
+80
to
+85
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. If a user clicks the "Stop Button" while the clock is still counting down, the "Stop Button" will "disappear" (and reappear when the clock reaches 0). Is this a bug of feature? |
||
|
||
// Initialize when the page first loads | ||
window.onload = function () { | ||
timeRemainingTitle.innerText = '00:00'; | ||
alarmSound.pause(); | ||
alarmSound.currentTime = 0; | ||
stopButton.style.display = 'none'; | ||
}; | ||
</script> | ||
|
||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,125 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Title here</title> | ||
<script defer src="quotes.js"></script> | ||
</head> | ||
<body> | ||
<h1>hello there</h1> | ||
<p id="quote"></p> | ||
<p id="author"></p> | ||
<button type="button" id="new-quote">New quote</button> | ||
</body> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Random Quote Generator</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
text-align: center; | ||
margin-top: 50px; | ||
} | ||
|
||
#quote-container { | ||
font-size: 24px; | ||
margin-bottom: 20px; | ||
padding: 20px; | ||
border: 2px solid #333; | ||
background-color: #f4f4f4; | ||
display: inline-block; | ||
max-width: 500px; | ||
text-align: center; | ||
} | ||
|
||
#quote { | ||
font-style: italic; | ||
margin-bottom: 10px; | ||
padding: 10px; | ||
border: 2px dashed #333; | ||
background-color: #fff; | ||
font-size: 26px; | ||
} | ||
|
||
#author { | ||
font-size: 20px; | ||
color: #555; | ||
padding: 10px; | ||
border: 2px solid #333; | ||
background-color: #e0e0e0; | ||
} | ||
|
||
button { | ||
font-size: 18px; | ||
padding: 10px 20px; | ||
margin-top: 20px; | ||
background-color: #4CAF50; | ||
color: white; | ||
border: none; | ||
cursor: pointer; | ||
} | ||
|
||
button:hover { | ||
background-color: #45a049; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<div id="quote-container"> | ||
<p id="quote">"Your quote will appear here."</p> | ||
<p id="author">- Author</p> | ||
</div> | ||
|
||
<button onclick="displayRandomQuote()">Get Random Quote</button> | ||
|
||
<script> | ||
// DO NOT EDIT BELOW HERE | ||
|
||
// pickFromArray is a function which will return one item, at | ||
// random, from the given array. | ||
// | ||
// Parameters | ||
// ---------- | ||
// choices: an array of items to pick from. | ||
// | ||
// Returns | ||
// ------- | ||
// One item at random from the given array. | ||
// | ||
// Examples of use | ||
// --------------- | ||
// pickFromArray(['a','b','c','d']) // maybe returns 'c' | ||
|
||
// You don't need to change this function | ||
function pickFromArray(choices) { | ||
return choices[Math.floor(Math.random() * choices.length)]; | ||
} | ||
|
||
// A list of quotes you can use in your app. | ||
// DO NOT modify this array, otherwise the tests may break! | ||
const quotes = [ | ||
{ | ||
quote: "Life isn't about getting and having, it's about giving and being.", | ||
author: "Kevin Kruse", | ||
}, | ||
{ | ||
quote: "Whatever the mind of man can conceive and believe, it can achieve.", | ||
author: "Napoleon Hill", | ||
}, | ||
{ | ||
quote: "Strive not to be a success, but rather to be of value.", | ||
author: "Albert Einstein", | ||
}, | ||
{ | ||
quote: | ||
"Two roads diverged in a wood, and I—I took the one less traveled by, And that has made all the difference.", | ||
author: "Robert Frost", | ||
}, | ||
{ | ||
quote: "I attribute my success to this: I never gave or took any excuse.", | ||
author: "Florence Nightingale", | ||
}, | ||
// Add more quotes here if needed | ||
]; | ||
|
||
// Function to display a random quote | ||
function displayRandomQuote() { | ||
const randomQuote = pickFromArray(quotes); // Pick a random quote from the array | ||
document.getElementById('quote').innerText = `"${randomQuote.quote}"`; // Display the quote with quotation marks | ||
document.getElementById('author').innerText = `- ${randomQuote.author}`; // Display the author | ||
} | ||
</script> | ||
|
||
</body> | ||
</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.
It is a better practice to separate JS code from HTML code. Can you transfer the JS code to
alarmclock.js
?