Skip to content

ITPLDN25 | Eyuel Abraham | Data Flows | programmer Humour week 3 #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fetch</title>
</head>
<body>
<div id="imageContainer"></div>

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

// Function to fetch the latest comic data
async function getImage() {
try {
const res = await fetch(`https://xkcd.now.sh/?comic=latest`);
if (!res.ok) throw new Error("Network response was not ok " + res.statusText);
const data = await res.json();

console.log(data);

renderImage(data.img);

} catch (error) {
console.error("Error fetching the data", error);
}
}

// Function to create and append the image element
function renderImage(imgUrl) {
const image = document.createElement('img');
image.src = imgUrl;
image.alt = "xkcd comic";
image.classList.add('comic-image');
imageSelector.appendChild(image);
}

// Call the getImage function to load and display the comic
getImage();
4 changes: 4 additions & 0 deletions fetch/programmer-humour/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.comic-image {
max-width: 100%;
height: auto;
}