-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
37 lines (29 loc) · 997 Bytes
/
app.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
async function fetchQuotes () {
try {
const response = await fetch('https://thecreativeindependent.com/api/v1/quotes.json')
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`)
}
return await response.json()
} catch (error) {
console.error(`Could not fetch quotes: ${error}. Using fallback quotes`)
const response = await fetch('./fallback_quotes.json')
return await response.json()
}
}
function chooseRandom (array) {
return array[Math.floor(Math.random() * array.length)]
}
function fillQuote (quote) {
const title = document.getElementById('quote__article-title')
document.getElementById('quote__text').innerHTML = quote.text
title.innerHTML = quote.post.title
title.href = quote.post.url
document.getElementById('quote').classList.remove('loading')
}
document.addEventListener('DOMContentLoaded', function () {
fetchQuotes().then(quotes => {
const quote = chooseRandom(quotes)
fillQuote(quote)
})
})