-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
36 lines (31 loc) · 1.08 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
async function fetchRandomQuote() {
try {
const response = await fetch('https://api.quotable.io/random');
if (response.status === 200) {
const { content, author } = await response.json();
return { content, author };
} else {
throw new Error('Request was not successful. Status code: ' + response.status);
}
} catch (error) {
throw error;
}
}
async function updateQuote() {
try {
const { content, author } = await fetchRandomQuote();
const quoteElement = document.getElementById('quote');
const authorElement = document.getElementById('author');
// Update the quote and author elements
quoteElement.textContent = content;
authorElement.textContent = author;
} catch (error) {
console.error('Error:', error);
}
}
// Attach the updateQuote function to the button click event
const generateButton = document.querySelector('#generate');
generateButton.addEventListener('click', updateQuote);
window.onload = function() {
updateQuote(); // Invoke the function directly, not updateQuote()
};