Skip to content

LONDON-JAN-25 | ANDREI FILIPPOV | Module-Data Flows | WEEK 3 feature/xkcd #200

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 5 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
17 changes: 17 additions & 0 deletions fetch/programmer-humour/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
const img = document.querySelector("img");
Copy link
Contributor

Choose a reason for hiding this comment

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

It is a good practice to append a suffix like "Elem" to a name for variables that store a DOM element.

const errorMessage = document.querySelector("#errorMessage");

const fetchData = async () => {
try {
const response = await fetch("https://xkcd.now.sh/?comic=latest");
const data = await response.json();
return data;
} catch (error) {
errorMessage.style.display = `contents`;
errorMessage.textContent = error;
}
};
fetchData().then((data) => {
img.setAttribute("src", data.img);
});
14 changes: 14 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>humour</title>
<link href="styles.css" rel="stylesheet" />
<script src="fetch.js" defer></script>
</head>
<body>
<div id="errorMessage"></div>
<img src="" />
Copy link
Contributor

Choose a reason for hiding this comment

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

Why assign an id attribute to one of the elements but not the other?

Copy link
Author

Choose a reason for hiding this comment

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

I did this because there’s only one image on the page, so I can select it by tag name. As for the div element, I wasn’t sure how many there might be in the future, so I decided to assign it an ID in advance.

</body>
</html>
9 changes: 9 additions & 0 deletions fetch/programmer-humour/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body {
display: flex;
justify-content: center;
align-items: center;
}
img {
margin: auto;
margin-top: 30vh;
}