Skip to content

Commit 12b6bb4

Browse files
Implement random quote display logic to avoid repetition and enhance autoplay functionality
1 parent fab5f08 commit 12b6bb4

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

Sprint-3/quote-generator/quotes.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,25 +499,40 @@ const autoplayToggle = document.querySelector("#autoplay-toggle");
499499
const autoplayStatus = document.querySelector("#autoplay-status");
500500

501501
let autoplayId = null;
502+
let currentQuote = null;
502503

504+
// Picks and displays a random quote, avoiding the same quote twice in a row.
503505
function showRandomQuote() {
504-
const randomQuote = pickFromArray(quotes);
506+
let randomQuote = pickFromArray(quotes);
507+
508+
if (quotes.length > 1) {
509+
while (randomQuote === currentQuote) {
510+
randomQuote = pickFromArray(quotes);
511+
}
512+
}
513+
514+
currentQuote = randomQuote;
505515
quoteP.innerText = randomQuote.quote;
506516
authorP.innerText = randomQuote.author;
507517
}
508518

519+
// Updates the status text to reflect whether auto-play is on or off.
509520
function setAutoplayStatus(isEnabled) {
510521
autoplayStatus.innerText = isEnabled ? "auto-play:ON" : "auto-play:OFF";
511522
}
512523

524+
// Starts auto-play and immediately shows a new quote as user feedback.
513525
function startAutoplay() {
514526
if (autoplayId !== null) {
515527
clearInterval(autoplayId);
516528
}
529+
530+
showRandomQuote();
517531
autoplayId = setInterval(showRandomQuote, 60000);
518532
setAutoplayStatus(true);
519533
}
520534

535+
// Stops auto-play if it is running and updates the status text.
521536
function stopAutoplay() {
522537
if (autoplayId !== null) {
523538
clearInterval(autoplayId);
@@ -526,6 +541,7 @@ function stopAutoplay() {
526541
setAutoplayStatus(false);
527542
}
528543

544+
// Handles checkbox changes to start or stop auto-play.
529545
function handleAutoplayToggle() {
530546
if (autoplayToggle.checked) {
531547
startAutoplay();

0 commit comments

Comments
 (0)