-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
30 lines (25 loc) · 937 Bytes
/
script.js
File metadata and controls
30 lines (25 loc) · 937 Bytes
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
async function fetchRandomQuote() {
try {
const response = await fetch ('https://type.fit/api/quotes');
const data = await response.json();
const randomIndex = Math.floor(Math.random() * data.length);
const randomQuote = data[randomIndex];
return randomQuote;
} catch (error) {
console.log('Error fetching the quote:', error);
return null;
}
}
async function displayRandomQuote() {
const quoteContainer = document.getElementById('quote');
const authorContainer = document.getElementById('author');
const quote = await fetchRandomQuote();
if (quote) {
quoteContainer.textContent = `"${quote.text}"`;
authorContainer.textContent = `- ${quote.author || 'Unknown'}`;
} else {
quoteContainer.textContent = 'Failed to fetch a quote. Please try again later.';
authorContainer.textContent = '';
}
}
displayRandomQuote();