Skip to content

Commit b5f7f05

Browse files
committed
refactor: introduce getElement helper with error handling and replace direct DOM lookups
1 parent 5ac5f13 commit b5f7f05

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

Sprint-3/quote-generator/quotes.js

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

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

495-
const quoteEl = document.getElementById("quote");
496-
const authorEl = document.getElementById("author");
497-
const newQuoteBtn = document.getElementById("new-quote");
498-
const autoPlayToggle = document.getElementById("auto-play-toggle");
499-
const autoPlayStatus = document.getElementById("auto-play-status");
495+
function getElement(id) {
496+
const element = document.getElementById(id);
497+
if (!element) {
498+
throw new Error(`Element with id "${id}" not found in the HTML`);
499+
}
500+
return element;
501+
}
502+
503+
const quoteEl = getElement("quote");
504+
const authorEl = getElement("author");
505+
const newQuoteBtn = getElement("new-quote");
506+
const autoPlayToggle = getElement("auto-play-toggle");
507+
const autoPlayStatus = getElement("auto-play-status");
500508

501509
newQuoteBtn.addEventListener("click", showRandomQuote);
502510

0 commit comments

Comments
 (0)