Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Sprint-3/quote-generator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<title>Quote generator app</title>
<script defer src="quotes.js"></script>
</head>
<body>
<h1>hello there</h1>
<p id="quote"></p>
<p id="author"></p>
<button type="button" id="new-quote">New quote</button>
<h1>Quote Generator</h1>
<p id="quote-container"></p>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The quote-container element is not needed here

<p id="quote-text"></p>
<p id="quote-author"></p>
<button id="new-quote-button">New quote</button>
</body>
</html>
9 changes: 9 additions & 0 deletions Sprint-3/quote-generator/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@
function pickFromArray(choices) {
return choices[Math.floor(Math.random() * choices.length)];
}
function showRandomQuote() {
const randomQuote = pickFromArray(quotes);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add condition after that is randomQuote is available or not, if not , then return

const quoteText = document.getElementById("quote-text");
const quoteAuthor = document.getElementById("quote-author");
Comment on lines +24 to +25

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During initial load, if the script runs before the DOM is ready, getElementById may return null for these elements. This will cause a TypeError: Cannot set property 'innerText' of null when trying to access quoteText.innerText or quoteAuthor.innerText.

quoteText.innerText = `"${randomQuote.quote}"`;
quoteAuthor.innerText = `- ${randomQuote.author}`;
}

window.onload = showRandomQuote;
document.getElementById("new-quote-button").addEventListener("click", showRandomQuote);
Comment on lines +30 to +31

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The button element might not exist during first load when the JS script runs before the DOM element is ready. If new-quote-button doesn't exist when trying to add the event listener, it will result in:

TypeError: Cannot read property 'addEventListener' of null

Please wrap the event listener setup inside window.onload and add a null check:

window.onload = function() {
showRandomQuote();
const button = document.getElementById("new-quote-button");
if (button) {
button.addEventListener("click", showRandomQuote);
}
};

This ensures the DOM is fully loaded before accessing elements and prevents the TypeError.

// A list of quotes you can use in your app.
// DO NOT modify this array, otherwise the tests may break!
const quotes = [
Expand Down