Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>XKCD Latest Comic</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<main>
<h1>Latest XKCD Comic</h1>
<div id="comic-container" aria-live="polite">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aria-live="polite"
Very well done here. This is a really good use of an accessibility feature and just forward-thinking in general. I wouldn't have thought to use it here for this assignment but it's definitely a useful spot to put it. Well done!

<p class="loading">Loading…</p>
</div>
</main>
<script src="script.js"></script>
</body>
</html>
33 changes: 33 additions & 0 deletions fetch/programmer-humour/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const API_URL = "https://xkcd.now.sh/?comic=latest";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very well done here for exporting this to a variable. It's aways helpful to the reviewer!


async function getLatestComic() {
const container = document.getElementById("comic-container");

try {
const response = await fetch(API_URL);

if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}

const data = await response.json();
console.log(data);

if (!data.img) {
throw new Error("API did not return an image");
}

container.innerHTML = "";

const img = document.createElement("img");
img.src = data.img;
img.alt = data.alt || data.title || "XKCD comic";

container.appendChild(img);
} catch (error) {
console.error("Fetch error:", error);
container.innerHTML = `<p>Could not load the comic. Please try again later.</p>`;
}
}

getLatestComic();
25 changes: 25 additions & 0 deletions fetch/programmer-humour/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
margin: 0;
padding: 20px;
line-height: 1.6;
}

h1 {
text-align: center;
}

#comic-container {
display: flex;
flex-direction: column;
align-items: center;
max-width: 900px;
margin: 20px auto;
}

#comic-container img {
max-width: 100%;
border: 2px solid #333;
border-radius: 8px;
}