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
4 changes: 2 additions & 2 deletions Sprint-3/quote-generator/index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!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>
Expand Down
2 changes: 1 addition & 1 deletion Sprint-3/quote-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "quote-generator",
"version": "1.0.0",
"license": "CC-BY-SA-4.0",
"description": "You must update this package",
"description": "Simple quote generator that picks a quote at random",
"scripts": {
"test": "jest --config=../jest.config.js quote-generator"
},
Expand Down
33 changes: 33 additions & 0 deletions Sprint-3/quote-generator/quotes.js
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 +28 to +29

Copy link
Copy Markdown
Contributor

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.

}
Comment on lines +13 to +30

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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).


showRandomQuote();

newQuoteBtn.addEventListener("click", showRandomQuote);
});
// pickFromArray is a function which will return one item, at
// random, from the given array.
//
Expand Down
Loading