diff --git a/fetch/programmer-humour/index.html b/fetch/programmer-humour/index.html new file mode 100644 index 00000000..09736e93 --- /dev/null +++ b/fetch/programmer-humour/index.html @@ -0,0 +1,18 @@ + + + + + + XKCD Latest Comic + + + +
+

Latest XKCD Comic

+
+

Loading…

+
+
+ + + diff --git a/fetch/programmer-humour/script.js b/fetch/programmer-humour/script.js new file mode 100644 index 00000000..a78dfe2d --- /dev/null +++ b/fetch/programmer-humour/script.js @@ -0,0 +1,33 @@ +const API_URL = "https://xkcd.now.sh/?comic=latest"; + +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 = `

Could not load the comic. Please try again later.

`; + } +} + +getLatestComic(); diff --git a/fetch/programmer-humour/style.css b/fetch/programmer-humour/style.css new file mode 100644 index 00000000..ee1feeb2 --- /dev/null +++ b/fetch/programmer-humour/style.css @@ -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; +}