-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | Zadri Abdule | Sprint 3 | Quote-generator #1328
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,38 @@ | ||
| // DO NOT EDIT BELOW HERE | ||
| document.addEventListener("DOMContentLoaded", () => { | ||
| const quoteEl = document.getElementById("quote"); | ||
| const authorEl = document.getElementById("author"); | ||
| const newQuoteBtn = document.getElementById("new-quote"); | ||
|
|
||
| if (!quoteEl || !authorEl || !newQuoteBtn) { | ||
| console.warn("Missing #quote, #author or #new-quote element."); | ||
| return; | ||
| } | ||
| let lastIndex = -1; | ||
|
|
||
| function showRandomQuote() { | ||
| let index; | ||
|
|
||
| if (quotes.length === 1) { | ||
| index = 0; | ||
| } else { | ||
| do { | ||
| index = Math.floor(Math.random() * quotes.length); | ||
| } while (index === lastIndex); | ||
| } | ||
|
|
||
| lastIndex = index; | ||
|
|
||
| const chosen = quotes[index]; | ||
|
|
||
| quoteEl.textContent = `"${chosen.quote}"`; | ||
| authorEl.textContent = `— ${chosen.author}`; | ||
| } | ||
|
Comment on lines
+13
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could probably separate the logic of selecting a random index (lines 14 to 24) from the presentation logic (lines 26-29).
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the feedback, appreciate it. |
||
|
|
||
| showRandomQuote(); | ||
|
|
||
| newQuoteBtn.addEventListener("click", showRandomQuote); | ||
| }); | ||
| // pickFromArray is a function which will return one item, at | ||
| // random, from the given array. | ||
| // | ||
|
|
||
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
"and the leading—appear to be for styling purposes. Keeping them in the HTML or in CSS could make it easier to style or modify the view. This allows front-end developers to adjust the UI without changing any JavaScript code.