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
21 changes: 21 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Programmer Humour</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<main class="container">
<h1>Programmer Humour</h1>
<p>Latest XKCD comic fetched from an API.</p>

<section id="comic-container" class="comic-container">
<p>Loading comic...</p>
</section>
</main>

<script src="script.js"></script>
</body>
</html>
48 changes: 48 additions & 0 deletions fetch/programmer-humour/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const comicContainer = document.getElementById("comic-container");

async function fetchLatestComic() {
comicContainer.innerHTML = "<p>Loading comic...</p>";

try {
const response = await fetch("https://xkcd.now.sh/?comic=latest");

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

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

renderComic(comicData);
} catch (error) {
console.error("Failed to fetch comic:", error);
showError("Sorry, the comic could not be loaded. Please try again later.");
}
}

function renderComic(comicData) {
comicContainer.innerHTML = "";

const title = document.createElement("h2");
title.className = "comic-title";
title.textContent = comicData.safe_title || "Latest XKCD Comic";

const image = document.createElement("img");
image.src = comicData.img;
image.alt = comicData.alt || comicData.safe_title || "XKCD comic";

comicContainer.appendChild(title);
comicContainer.appendChild(image);
}

function showError(message) {
comicContainer.innerHTML = "";

const errorText = document.createElement("p");
errorText.className = "error-message";
errorText.textContent = message;

comicContainer.appendChild(errorText);
}

fetchLatestComic();
36 changes: 36 additions & 0 deletions fetch/programmer-humour/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
body {
margin: 0;
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #222;
}

.container {
max-width: 900px;
margin: 0 auto;
padding: 2rem 1rem;
text-align: center;
}

.comic-container {
margin-top: 2rem;
padding: 1.5rem;
background-color: white;
border: 1px solid #ddd;
border-radius: 8px;
}

.comic-container img {
max-width: 100%;
height: auto;
border-radius: 6px;
}

.comic-title {
margin-bottom: 1rem;
}

.error-message {
color: #b00020;
font-weight: bold;
}
Loading