Skip to content

Commit 4c94c76

Browse files
committed
Recreate programmer humour task clean
1 parent 3a422e9 commit 4c94c76

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

fetch/programmer-humour/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Programmer Humour</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
<body>
9+
10+
<h1>Latest XKCD Comic</h1>
11+
12+
<div id="comic-container">
13+
<p id="loading">Loading comic...</p>
14+
</div>
15+
16+
<script src="script.js"></script>
17+
</body>
18+
</html>

fetch/programmer-humour/script.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function fetchComic() {
2+
const container = document.getElementById("comic-container");
3+
const loading = document.getElementById("loading");
4+
5+
fetch("https://xkcd.now.sh/?comic=latest")
6+
.then(response => {
7+
if (!response.ok) {
8+
throw new Error("Network response was not ok");
9+
}
10+
return response.json();
11+
})
12+
.then(data => {
13+
console.log(data);
14+
15+
if (loading) loading.remove();
16+
17+
container.innerHTML = `
18+
<h2>${data.title}</h2>
19+
<img src="${data.img}" alt="${data.alt}" />
20+
<p>${data.alt}</p>
21+
`;
22+
})
23+
.catch(error => {
24+
console.error(error);
25+
26+
if (loading) {
27+
loading.textContent = "Error loading comic 😢";
28+
} else {
29+
container.innerHTML = `<p>Error loading comic 😢</p>`;
30+
}
31+
});
32+
}
33+
34+
window.addEventListener("load", fetchComic);

fetch/programmer-humour/style.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
text-align: center;
4+
background-color: #f4f4f4;
5+
}
6+
7+
img {
8+
max-width: 100%;
9+
height: auto;
10+
margin-top: 20px;
11+
}

0 commit comments

Comments
 (0)