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
35 changes: 22 additions & 13 deletions Sprint-3/quote-generator/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
<!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>
<p id="quote"></p>
<p id="author"></p>
<button type="button" id="new-quote">New quote</button>
</body>
</html>

<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" />
</head>

<body>
<main class="quote-container">
<blockquote class="quote-box">
<p id="quote"></p>
<p id="author"></p>
</blockquote>
<button id="new-quote">New quote</button>
</main>

<!-- Connect your JavaScript file -->
<script src="quotes.js"></script>
</body>

</html>
22 changes: 22 additions & 0 deletions Sprint-3/quote-generator/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,25 @@ const quotes = [
];

// call pickFromArray with the quotes array to check you get a random quote

// Function to select a random quote and update the HTML elements
function displayNewQuote() {
const quoteP = document.getElementById("quote");
const authorP = document.getElementById("author");

// Get a random quote object from the quotes array
const randomQuote = pickFromArray(quotes);

// Update the DOM element contents
quoteP.textContent = randomQuote.quote;
authorP.textContent = randomQuote.author;
}

// 1. Display a random quote when the page initially loads
displayNewQuote();

// 2. Add a click event listener to the "New quote" button
const newQuoteBtn = document.querySelector("#new-quote");
if (newQuoteBtn) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you use an if check here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i used it to serve as a form of guardrail instruction - Only attach the event listener if the button actually exists on the page. If it doesn't, skip it and keep running the rest of the app safely.

checking online there are other ways i can achieve the same effect, do you think i should use another method?

newQuoteBtn.addEventListener("click", displayNewQuote);
}
Loading