-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
25 lines (19 loc) · 852 Bytes
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const quote = document.querySelector("#quote");
const author = document.querySelector("#author");
const newQuoteButton = document.querySelector("#new-quote");
const tweetButton = document.querySelector("#twitter-button")
const fetchQuote = async () => {
const response = await fetch("https://api.quotable.io/random");
const randomQuote = await response.json();
if (!response.ok) {
alert("Error getting the quote, please try again later.")
}
updateQuote(randomQuote);
}
const updateQuote = (randomQuote) => {
quote.innerHTML = `“${randomQuote.content}”`;
author.innerHTML = `-${randomQuote.author}`;
tweetButton.href =`https://twitter.com/intent/tweet/?text=${encodeURIComponent(randomQuote.content + ` - ` + randomQuote.author)}`;
}
newQuoteButton.addEventListener("click", fetchQuote);
fetchQuote();