Skip to content

Commit ea60dd5

Browse files
Refactor quote selection logic into a reusable function and update event listeners for better readability
1 parent 17571ba commit ea60dd5

1 file changed

Lines changed: 13 additions & 28 deletions

File tree

Sprint-3/quote-generator/quotes.js

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -492,43 +492,28 @@ const quotes = [
492492

493493
// call pickFromArray with the quotes array to check you get a random quote
494494

495-
const randomQuote = quotes[Math.floor(Math.random()*quotes.length)];
496-
497-
const quote = document.getElementById("quote");
498-
quote.innerText = randomQuote.quote;
499-
500-
const author = document.getElementById("author");
501-
author.innerText = randomQuote.author;
502-
503-
const button = document.getElementById("new-quote");
504-
button.addEventListener("click", () => {
505-
const randomQuote =quotes[Math.floor(Math.random()*quotes.length)];
495+
function chooseQuote() {
496+
const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
506497

507498
const quote = document.getElementById("quote");
508499
quote.innerText = randomQuote.quote;
509500

510501
const author = document.getElementById("author");
511502
author.innerText = randomQuote.author;
512-
});
503+
}
513504

514-
function chooseQuote(){
515-
const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
505+
window.addEventListener("load", chooseQuote);
516506

517-
const quote = document.getElementById("quote");
518-
quote.innerText = randomQuote.quote;
507+
const button = document.getElementById("new-quote");
508+
button.addEventListener("click", chooseQuote);
519509

520-
const author = document.getElementById("author");
521-
author.innerText = randomQuote.author;
522-
}
523510
const autoGenerate = document.getElementById("auto-play-toggle");
524511
let interval = null;
525-
autoGenerate.addEventListener("change",()=>{
526-
527-
if(autoGenerate.checked){
528-
interval=setInterval(chooseQuote,2000)
529-
}else{
530-
clearInterval(interval)
531-
interval=null
512+
autoGenerate.addEventListener("change", () => {
513+
if (autoGenerate.checked) {
514+
interval = setInterval(chooseQuote, 2000);
515+
} else {
516+
clearInterval(interval);
517+
interval = null;
532518
}
533-
})
534-
519+
});

0 commit comments

Comments
 (0)