diff --git a/fetch/programmer-humour/index.html b/fetch/programmer-humour/index.html new file mode 100644 index 00000000..a2270150 --- /dev/null +++ b/fetch/programmer-humour/index.html @@ -0,0 +1,13 @@ + + + + + + Programming Humor - XKCD + + + + +

+ + diff --git a/fetch/programmer-humour/script.js b/fetch/programmer-humour/script.js new file mode 100644 index 00000000..d12bee6b --- /dev/null +++ b/fetch/programmer-humour/script.js @@ -0,0 +1,25 @@ +async function fetchXKCDComic() { + const url = "https://xkcd.now.sh/?comic=latest"; + const response = await fetch(url); + if (!response.ok) { + throw new Error(`Response status: ${response.status}`); + } + return response.json(); +} + +async function displayComic() { + const comicDisplay = document.querySelector("img"); + const errorDisplay = document.querySelector("p"); + + try { + const data = await fetchXKCDComic(); + console.log(data); + comicDisplay.src = data.img; + comicDisplay.alt = data.alt; + comicDisplay.title = data.title; + } catch (error) { + errorDisplay.textContent = error.message; + } +} + +document.addEventListener("DOMContentLoaded", displayComic); diff --git a/fetch/programmer-humour/styles.css b/fetch/programmer-humour/styles.css new file mode 100644 index 00000000..e69de29b