Skip to content

Commit bcde3aa

Browse files
committed
Complete quote generator - all tests pass
1 parent 71d9abc commit bcde3aa

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

‎Sprint-3/quote-generator/quotes.js‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,93 @@
1+
const newQuoteBtn = document.querySelector("#new-quote");
2+
const quote = document.querySelector("#quote");
3+
const author = document.querySelector("#author");
4+
const autoPlayToggle = document.querySelector("#auto-play-toggle");
5+
const autoPlayStatus = document.querySelector("#auto-play-status");
6+
7+
let seenQuotes = new Set();
8+
let autoPlayIntervalDuration = 10000; // 10 second interval duration
9+
let autoPlayIntervalId = null;
10+
let countdownIntervalId = null;
11+
12+
// only show quotes that have not yet been seen
13+
const getUnseenQuotes = () => {
14+
// clear seenQuotes if all quotes have been displayed
15+
if (seenQuotes.size === quotes.length) seenQuotes.clear();
16+
17+
// loop until index is not in seenQuotes
18+
let index;
19+
do {
20+
index = Math.floor(Math.random() * quotes.length);
21+
} while (seenQuotes.has(index));
22+
23+
seenQuotes.add(index);
24+
return quotes[index];
25+
};
26+
27+
const displayQuote = () => {
28+
const currentQuote = getUnseenQuotes();
29+
quote.innerText = currentQuote.quote;
30+
author.innerText = currentQuote.author;
31+
32+
if (autoPlayIntervalId) startCountDown();
33+
};
34+
35+
const startCountDown = () => {
36+
let timeLeft = autoPlayIntervalDuration / 1000;
37+
autoPlayStatus.innerText = `auto-play:ON - Quote will change in ${timeLeft} seconds`;
38+
39+
if (countdownIntervalId) clearInterval(countdownIntervalId);
40+
41+
countdownIntervalId = setInterval(() => {
42+
timeLeft--;
43+
autoPlayStatus.innerText = `auto-play:ON - Quote will change in ${timeLeft} seconds`;
44+
if (timeLeft === 0) {
45+
clearInterval(countdownIntervalId);
46+
countdownIntervalId = null;
47+
}
48+
}, 1000);
49+
};
50+
51+
const runInterval = () => {
52+
displayQuote();
53+
startCountDown();
54+
autoPlayIntervalId = setInterval(displayQuote, autoPlayIntervalDuration);
55+
};
56+
57+
const startAutoPlay = () => {
58+
autoPlayStatus.innerText = "auto-play:ON";
59+
newQuoteBtn.classList.add("disabled");
60+
newQuoteBtn.disabled = true;
61+
runInterval();
62+
};
63+
64+
const stopAutoPlay = () => {
65+
if (countdownIntervalId) {
66+
clearInterval(countdownIntervalId);
67+
countdownIntervalId = null;
68+
}
69+
70+
if (autoPlayIntervalId) {
71+
clearInterval(autoPlayIntervalId);
72+
autoPlayIntervalId = null;
73+
}
74+
75+
autoPlayStatus.innerText = "";
76+
newQuoteBtn.classList.remove("disabled");
77+
newQuoteBtn.disabled = false;
78+
};
79+
80+
// event listeners
81+
autoPlayToggle.addEventListener("change", () => {
82+
if (autoPlayToggle.checked) {
83+
startAutoPlay();
84+
} else {
85+
stopAutoPlay();
86+
}
87+
});
88+
document.addEventListener("DOMContentLoaded", displayQuote);
89+
newQuoteBtn.addEventListener("click", displayQuote);
90+
191
// DO NOT EDIT BELOW HERE
292

393
// pickFromArray is a function which will return one item, at

0 commit comments

Comments
 (0)