-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuoteGenerator.js
43 lines (31 loc) · 1.01 KB
/
QuoteGenerator.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
37
38
39
40
41
42
43
const spinner = document.querySelector('#js-spinner');
const newQuoteButton = document.querySelector('#js-new-quote');
newQuoteButton.addEventListener('click', getQuote);
const endpoint = 'https://api.quotable.io/random';
async function getQuote() {
try {
spinner.classList.remove('hidden');
newQuoteButton.disabled = true;
const response = await fetch(endpoint)
if (!response.ok) {
throw Error(response.statusText);
}
const json = await response.json();
displayQuote(json.content);
displayAuthor(json.author);
} catch {
alert('Failed to fetch new quote');
} finally {
newQuoteButton.disabled = false;
spinner.classList.add('hidden');
}
}
function displayQuote(quote) {
const quoteText = document.querySelector('#js-quote-text');
quoteText.textContent = quote;
}
function displayAuthor(author){
const quoteAuthor = document.querySelector('#js-quote-author');
quoteAuthor.textContent = '-' + author;
}
getQuote();