-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
36 lines (29 loc) · 1.01 KB
/
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
25
26
27
28
29
30
31
32
33
34
35
36
const quoteText = document.getElementById('quote-text');
const authorText = document.getElementById('author');
const newQuoteBtn = document.getElementById('new-quote');
const tweetBtn = document.getElementById('twitter-button');
// state
const apiUrl = 'https://type.fit/api/quotes';
let apiQuotes = [];
const getNewQuote = () => {
const quote = apiQuotes[Math.floor(Math.random() * apiQuotes.length)];
quoteText.textContent = quote.text;
if (quote.author === null) {
author.textContent = "Anon"
} else {
authorText.textContent = quote.author;
}
}
const getQuotes = async () => {
const response = await fetch(apiUrl);
const data = await response.json()
apiQuotes = data;
getNewQuote()
}
const tweet = () => {
const twitterUrl = `https://twitter.com/intent/tweet?text=${quoteText.textContent} - ${authorText.textContent}`;
window.open(twitterUrl, '_blank')
}
newQuoteBtn.addEventListener('click', getNewQuote);
tweetBtn.addEventListener('click', tweet);
getQuotes();