-
-
Notifications
You must be signed in to change notification settings - Fork 318
London | 26-ITP-May | Martin Mwaka | Sprint 3 | Quote generator #1275
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
d703354
85e8a9e
43108bd
71d9abc
bcde3aa
aaaf7f6
b7754f4
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,15 +1,26 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Title here</title> | ||
| <script defer src="quotes.js"></script> | ||
| </head> | ||
| <body> | ||
| <h1>hello there</h1> | ||
|
|
||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Quote generator app</title> | ||
| <link rel="stylesheet" href="style.css"> | ||
| <script defer src="quotes.js"></script> | ||
| </head> | ||
|
|
||
| <body> | ||
| <main> | ||
| <h1>Quote Generator</h1> | ||
| <p id="quote"></p> | ||
| <p id="author"></p> | ||
| <button type="button" id="new-quote">New quote</button> | ||
| </body> | ||
| </html> | ||
| <div class="controls"> | ||
| <input type="checkbox" id="auto-play-toggle" /> | ||
| <label for="auto-play-toggle">Auto-play</label> | ||
| </div> | ||
| <p id="auto-play-status"></p> | ||
| </main> | ||
| </body> | ||
|
|
||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,94 @@ | ||
| const newQuoteBtn = document.querySelector("#new-quote"); | ||
| const quote = document.querySelector("#quote"); | ||
| const author = document.querySelector("#author"); | ||
| const autoPlayToggle = document.querySelector("#auto-play-toggle"); | ||
| const autoPlayStatus = document.querySelector("#auto-play-status"); | ||
|
|
||
| let seenQuotes = new Set(); | ||
| const INTERVAL_DURATION_MS = 10000; // 10 second interval duration | ||
| const FULL_COUNTDOWN_CYCLE_MS = INTERVAL_DURATION_MS + 1000; // add extra second to enable countdown to reach 0 before quote changes | ||
| let autoPlayIntervalId = null; | ||
| let countdownIntervalId = null; | ||
|
|
||
| // only show quotes that have not yet been seen | ||
| const getUnseenQuotes = () => { | ||
|
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. This is a good idea, but is there an efficient way to get unseen quotes using a set that does not require a do-while loop? |
||
| // clear seenQuotes if all quotes have been displayed | ||
| if (seenQuotes.size === quotes.length) seenQuotes.clear(); | ||
|
|
||
| // loop until index is not in seenQuotes | ||
| let index; | ||
| do { | ||
| index = Math.floor(Math.random() * quotes.length); | ||
| } while (seenQuotes.has(index)); | ||
|
|
||
| seenQuotes.add(index); | ||
| return quotes[index]; | ||
| }; | ||
|
|
||
| const displayQuote = () => { | ||
| const currentQuote = getUnseenQuotes(); | ||
| quote.innerText = currentQuote.quote; | ||
| author.innerText = currentQuote.author; | ||
|
|
||
| if (autoPlayIntervalId) startCountDown(); | ||
| }; | ||
|
|
||
| const startCountDown = () => { | ||
| let timeLeft = INTERVAL_DURATION_MS / 1000; | ||
| autoPlayStatus.innerText = `auto-play:ON - Quote will change in ${timeLeft} seconds`; | ||
|
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. User feedback like this is nice, well done |
||
|
|
||
| if (countdownIntervalId) clearInterval(countdownIntervalId); | ||
|
|
||
| countdownIntervalId = setInterval(() => { | ||
| timeLeft--; | ||
| autoPlayStatus.innerText = `auto-play:ON - Quote will change in ${timeLeft} seconds`; | ||
| if (timeLeft === 0) { | ||
| clearInterval(countdownIntervalId); | ||
| countdownIntervalId = null; | ||
| } | ||
| }, 1000); | ||
| }; | ||
|
|
||
| const runInterval = () => { | ||
| displayQuote(); | ||
| startCountDown(); | ||
| autoPlayIntervalId = setInterval(displayQuote, FULL_COUNTDOWN_CYCLE_MS); | ||
| }; | ||
|
|
||
| const startAutoPlay = () => { | ||
| autoPlayStatus.innerText = "auto-play:ON"; | ||
| newQuoteBtn.classList.add("disabled"); | ||
| newQuoteBtn.disabled = true; | ||
| runInterval(); | ||
| }; | ||
|
|
||
| const stopAutoPlay = () => { | ||
| if (countdownIntervalId) { | ||
| clearInterval(countdownIntervalId); | ||
| countdownIntervalId = null; | ||
| } | ||
|
|
||
| if (autoPlayIntervalId) { | ||
| clearInterval(autoPlayIntervalId); | ||
| autoPlayIntervalId = null; | ||
| } | ||
|
|
||
| autoPlayStatus.innerText = ""; | ||
| newQuoteBtn.classList.remove("disabled"); | ||
| newQuoteBtn.disabled = false; | ||
| }; | ||
|
|
||
| // event listeners | ||
| autoPlayToggle.addEventListener("change", () => { | ||
| if (autoPlayToggle.checked) { | ||
| startAutoPlay(); | ||
| } else { | ||
| stopAutoPlay(); | ||
| } | ||
| }); | ||
| document.addEventListener("DOMContentLoaded", displayQuote); | ||
| newQuoteBtn.addEventListener("click", displayQuote); | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
| // pickFromArray is a function which will return one item, at | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,72 @@ | ||
| /** Write your CSS in here **/ | ||
| * { | ||
| box-sizing: border-box; | ||
| } | ||
|
|
||
| body { | ||
| display: grid; | ||
| place-items: center; | ||
| min-height: 100vh; | ||
| margin: 0; | ||
| /* background: linear-gradient(135deg, #667eea, #764ba2); */ | ||
| background: linear-gradient(135deg, #2749e1, #61269c); | ||
| font-family: Arial, sans-serif; | ||
| } | ||
|
|
||
| main { | ||
| width: min(90%, 600px); | ||
| padding: 40px; | ||
| background: white; | ||
| border-radius: 20px; | ||
| text-align: center; | ||
| box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2); | ||
| } | ||
|
|
||
| h1 { | ||
| margin-bottom: 30px; | ||
| color: #333; | ||
| } | ||
|
|
||
| #quote { | ||
| font-size: 1.5rem; | ||
| font-style: italic; | ||
| line-height: 1.3; | ||
| color: #444; | ||
| } | ||
|
|
||
| #author { | ||
| margin-top: 20px; | ||
| font-size: 1.1rem; | ||
| color: #777; | ||
| } | ||
|
|
||
| button { | ||
| margin-top: 30px; | ||
| padding: 12px 25px; | ||
| border: none; | ||
| border-radius: 25px; | ||
| background: #3958e3; | ||
| color: white; | ||
| font-size: 1rem; | ||
| cursor: pointer; | ||
| } | ||
|
|
||
| button.disabled { | ||
| background-color: #aaa; | ||
| cursor: not-allowed; | ||
| opacity: 0.7; | ||
| } | ||
|
|
||
| button:hover { | ||
| background: #5563c1; | ||
| } | ||
|
|
||
| .controls { | ||
| margin-top: 25px; | ||
| } | ||
|
|
||
| #auto-play-status, | ||
| #countdown { | ||
| color: #666; | ||
| font-size: 0.9rem; | ||
| } |
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.
If you are using IDs, is there a better method to use than querySelector?