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
2 changes: 1 addition & 1 deletion Sprint-3/quote-generator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title here</title>
<title>Quote Generator</title>
<script defer src="quotes.js"></script>
</head>
<body>
Expand Down
24 changes: 24 additions & 0 deletions Sprint-3/quote-generator/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,27 @@ const quotes = [
];

// call pickFromArray with the quotes array to check you get a random quote
// console.log(pickFromArray(quotes))

// function to display random quote on the page
function displayRandomQuote(){

// get the html ID elements where we want to show author's name and quote's
const authorElements = document.getElementById("author");
const quoteElements = document.getElementById("quote")

// use the "pickFromArray()" function to pick one quote randomly from the array to display each time it runs
const randomQuote = pickFromArray(quotes);

// update the display with the selected author and quote by setting the text into there html elements
quoteElements.textContent = randomQuote.quote;
authorElements.textContent = randomQuote.author;
}
// use "DOMContentloaded" to ensure all html elements are loaded before updating the DOM to prevent returning null
document.addEventListener("DOMContentLoaded", function(){

displayRandomQuote(); // calls a random quote on page load

const newQuoteButton = document.getElementById("new-quote"); // gets the html element for the button new-quote
newQuoteButton.addEventListener("click", displayRandomQuote); // shows a a random new quote each time the buttons clicked
});