Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -3,11 +3,11 @@
<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>Random Quote:</h1>
<p id="quote"></p>
<p id="author"></p>
<button type="button" id="new-quote">New quote</button>
Expand Down
31 changes: 31 additions & 0 deletions Sprint-3/quote-generator/quotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,34 @@ const quotes = [
];

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

function setup() {
updateQuote();
appendQuote();
}

const state = {
quote: "",
author: "",
};

function updateQuote() {
const chosenQuote = pickFromArray(quotes);
state.quote = chosenQuote.quote;
state.author = chosenQuote.author;
}

function appendQuote() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Append isn't the most clear name here - it suggests that each time you call it an additional quote will be added to the page, rather than replacing the existing quote.

(Even though you implementation does use append internally, the effect of the function isn't to append something to the page).

Can you think of a more clear name?

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.

renamed to replaceQuote

const quoteElem = document.getElementById("quote");
const authorElem = document.getElementById("author");
quoteElem.textContent = "";
authorElem.textContent = "";
quoteElem.append(state.quote);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This works, but you could also directly set textContent to this value:

quoteElem.textContent = state.quote

which may be more clear as to your intent when reading, and may avoid a brief flash of an empty quote in the middle.

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.

instead of resetting .textcontent to "" and appending new quote/author, replaced quote/author directly with .textcontent
and removed append lines

authorElem.append(state.author);
}

document.getElementById("new-quote").addEventListener("click", function () {
setup();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

setup isn't a very clear name for this action. It makes sense on load that this is what you're doing.

Personally I'd probably just have one function in file: showNewQuote, and have that be called both onload and on button click.

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.

renamed function to showNewQuote

});

window.onload = setup;
Loading