-
-
Notifications
You must be signed in to change notification settings - Fork 193
Glasgow | ITP-SEP-25 | Alaa Tagi | Sprint 3 | feature/quote-generator #890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
jubelAhmed
left a comment
There was a problem hiding this comment.
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 JS script run before the DOM is ready?
Now fix all the comments
| <p id="author"></p> | ||
| <button type="button" id="new-quote">New quote</button> | ||
| <h1>Quote Generator</h1> | ||
| <p id="quote-container"></p> |
There was a problem hiding this comment.
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
| return choices[Math.floor(Math.random() * choices.length)]; | ||
| } | ||
| function showRandomQuote() { | ||
| const randomQuote = pickFromArray(quotes); |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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.
| window.onload = showRandomQuote; | ||
| document.getElementById("new-quote-button").addEventListener("click", showRandomQuote); |
There was a problem hiding this comment.
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-O-Emmanuel
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your quote generator app works as intended, but please go through this article 10 Code comments best practices for developers.
Learners, PR Template
Self checklist
Changelist
I have completed all the tasks that required in this sprint.
Questions
No questions.