-
-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathscript.js
More file actions
25 lines (23 loc) · 741 Bytes
/
script.js
File metadata and controls
25 lines (23 loc) · 741 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
async function getComic() {
try {
// Fetch the latest comic from the API
const response = await fetch("https://xkcd.now.sh/?comic=latest");
// Convert the response to JSON
const data = await response.json();
// Log the data to the console
console.log(data);
// Grab the comic container from the DOM
let comicDiv = document.getElementById("comic");
// Create an img element
let imgComic = document.createElement("img");
// Set the src of the img to the comic image URL
imgComic.src = data.img;
// Append the img to the comic container
comicDiv.appendChild(imgComic);
} catch (error) {
// Catch and log any errors
console.log(error);
}
}
// Call the function
getComic();