Skip to content

Commit efe814d

Browse files
committed
quotes function been upadated
1 parent 9158842 commit efe814d

3 files changed

Lines changed: 60 additions & 34 deletions

File tree

Sprint-3/quote-generator/index.html

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@
1111
<div class="header">
1212
<h1>Quote Generator</h1>
1313
</div>
14-
1514
<div class="quote-box">
16-
<p id="quote"></p>
17-
<p id="author"></p>
15+
<p>" <span id="quote"></span> "</p>
16+
<p>-- <span id="author"></span> --</p>
1817
</div>
19-
2018
<div class="bottom-controls">
2119
<div class="button">
2220
<button type="button" id="new-quote">New quote</button>
2321
</div>
2422
<div class="autoplay-container">
2523
<label for="autoplay">
2624
<input type="checkbox" id="autoplay" />
27-
Auto-play
25+
Auto-play :
2826
</label>
29-
<span id="autoplay-status">Auto-play: OFF</span>
27+
<span id="autoplay-status"> OFF</span>
3028
</div>
3129
</div>
3230
</div>

Sprint-3/quote-generator/quotes.js

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -487,51 +487,74 @@ const quotes = [
487487

488488
// call pickFromArray with the quotes array to check you get a random quote
489489

490-
// Function to pick a random quote
491-
function pickFromArray() {
490+
// Picks a random quote object from the quotes array
491+
function pickFromArray(quotes) {
492+
const randomIndex = Math.floor(Math.random() * quotes.length);
493+
return quotes[randomIndex];
494+
}
495+
496+
// Renders a quote object into the DOM
497+
function renderQuote(quote) {
492498
const quoteEl = document.getElementById("quote");
493499
const authorEl = document.getElementById("author");
494-
const randomIndex = Math.floor(Math.random() * quotes.length);
495500

496-
quoteEl.textContent = `" ${quotes[randomIndex].quote} "`;
497-
authorEl.textContent = `-- ${quotes[randomIndex].author} --`;
498-
return randomIndex;
501+
if (!quoteEl || !authorEl || !quote) return;
502+
503+
quoteEl.textContent = quote.quote;
504+
authorEl.textContent = quote.author;
499505
}
500506

501-
// Setup function for browser only
507+
// Sets up the application, event listeners, and initial state
502508
function setupQuoteApp() {
503509
const button = document.getElementById("new-quote");
504510
const autoplayCheckbox = document.getElementById("autoplay");
505511
const autoplayStatus = document.getElementById("autoplay-status");
506512

507-
// Don't call pickFromArray here for Jest test
508-
if (typeof window !== "undefined" && window.document) {
509-
pickFromArray();
513+
if (!button || !autoplayCheckbox || !autoplayStatus) return;
514+
515+
let autoplayInterval = null;
510516

511-
button.addEventListener("click", () => pickFromArray());
517+
// Show first quote on page load
518+
renderQuote(pickFromArray(quotes));
512519

513-
let autoplayInterval = null;
514-
autoplayCheckbox.addEventListener("change", () => {
515-
if (autoplayCheckbox.checked) {
516-
autoplayInterval = setInterval(pickFromArray, 5000);
517-
autoplayStatus.textContent = "Auto-play: ON";
518-
autoplayStatus.style.color = "#4CAF50";
519-
} else {
520-
clearInterval(autoplayInterval);
521-
autoplayInterval = null;
522-
autoplayStatus.textContent = "Auto-play: OFF";
523-
autoplayStatus.style.color = "brown";
524-
}
525-
});
520+
// Handles generating and rendering a new quote
521+
function handleNewQuote() {
522+
renderQuote(pickFromArray(quotes));
526523
}
524+
525+
// Button click generates a new quote
526+
button.addEventListener("click", handleNewQuote);
527+
528+
// Toggle autoplay feature
529+
autoplayCheckbox.addEventListener("change", () => {
530+
if (autoplayCheckbox.checked) {
531+
// Start auto-changing quotes every 5 seconds
532+
autoplayInterval = setInterval(handleNewQuote, 5000);
533+
autoplayStatus.textContent = "ON";
534+
535+
// Apply ON styling via CSS class
536+
autoplayStatus.classList.add("autoplay-on");
537+
autoplayStatus.classList.remove("autoplay-off");
538+
} else {
539+
// Stop auto-changing quotes
540+
clearInterval(autoplayInterval);
541+
autoplayInterval = null;
542+
543+
autoplayStatus.textContent = "OFF";
544+
545+
// Apply OFF styling via CSS class
546+
autoplayStatus.classList.add("autoplay-off");
547+
autoplayStatus.classList.remove("autoplay-on");
548+
}
549+
});
527550
}
528551

529-
// Only run setup in browser environment
552+
// Initialize app only when DOM is fully loaded (browser environment)
530553
if (typeof window !== "undefined") {
531554
window.addEventListener("DOMContentLoaded", setupQuoteApp);
532555
}
533556

534-
// Export function for Jest tests
557+
// Export function for Jest testing environment
535558
if (typeof module !== "undefined") {
536559
module.exports = pickFromArray;
537560
}

Sprint-3/quote-generator/style.css

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ body {
1616
}
1717

1818
.container {
19-
background: rgba(221, 221, 221, 0.4);
19+
background: rgb(206, 202, 202);
2020
padding: 10px;
2121
border-radius: 16px;
2222
width: 100%;
@@ -112,9 +112,14 @@ body {
112112
cursor: pointer;
113113
}
114114

115-
#autoplay-status {
115+
.autoplay-on {
116+
color: #4caf50;
116117
font-weight: bold;
118+
}
119+
120+
.autoplay-off {
117121
color: brown;
122+
font-weight: bold;
118123
}
119124

120125
/* Responsive */

0 commit comments

Comments
 (0)