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
12 changes: 9 additions & 3 deletions Sprint-3/quote-generator/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<!DOCTYPE html>
<!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>
<title>Quote Generator App</title>
<script defer src="quotes.js"></script>
</head>
<body>
<h1>hello there</h1>
<h1>Quote Generator</h1>
<p id="quote"></p>
<p id="author"></p>
<button type="button" id="new-quote">New quote</button>
<div id="autoplay-controls">
<label>
<input type="checkbox" id="autoplay-toggle" /> Enable Auto-play
</label>
<p id="autoplay-status">auto-play: OFF</p>
</div>
</body>
</html>
42 changes: 42 additions & 0 deletions Sprint-3/quote-generator/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,45 @@ const quotes = [
];

// call pickFromArray with the quotes array to check you get a random quote
// Select the elements from the html
const quoteDisplay = document.getElementById("quote");
const authorDisplay = document.getElementById("author");
const newQuoteButton = document.getElementById("new-quote");

// Create a function to update the content on the screen
function updateQuote() {
// Use the provided pickFromArray function
const randomQuoteObject = pickFromArray(quotes);
// Access the 'quote' property from the object and,
// inject it into the HTML element
quoteDisplay.innerText = randomQuoteObject.quote;
// Access the 'author' property and use a template literal,
// to add a dash for styling
authorDisplay.innerText = `- ${randomQuoteObject.author}`;
}

// Add event listener to the button
newQuoteButton.addEventListener("click", updateQuote);
// Show a random quote immediately when the page loads
updateQuote();

// Get the new elements from the DOM
const autoplayToggle = document.getElementById("autoplay-toggle");
const autoplayStatus = document.getElementById("autoplay-status");

// Track timer
let timerId = null;

// Event listener to the checkbox
autoplayToggle.addEventListener("change", () => {
if (autoplayToggle.checked) {
// Switch is ON
autoplayStatus.innerText = "auto-play: ON";
timerId = setInterval(updateQuote, 5000);
} else {
// Switch is OFF
autoplayStatus.innerText = "auto-play: OFF";
// Stop the timer
clearInterval(timerId);
}
});