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
16 changes: 16 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>XKCD Comic</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="comic">
<h2 id="title"></h2>
<img id="image" alt="">
<p id="alt"></p>
</div>
<script src="script.js"></script>
</body>
</html>
21 changes: 21 additions & 0 deletions fetch/programmer-humour/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const title = document.getElementById("title");
const image = document.getElementById("image");
const alt = document.getElementById("alt");

async function getImg() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do you think this function name clearly describes what it does?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

maybe getImage could be better?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I was mostly thinking here that it in addition to getting image it sets values for some text fields, but this is just a nitpick.

const url = "https://xkcd.now.sh/?comic=latest";
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice use of exception here!

}
const result = await response.json();
title.textContent = result.safe_title;
image.src = result.img;
alt.textContent = result.alt;
console.log(result);
} catch (error) {
console.log(error.message);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is there a better function to log errors?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

oh yes, console.error

}
}
getImg();
9 changes: 9 additions & 0 deletions fetch/programmer-humour/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#comic {
text-align: center;
max-width: 800px;
margin: 0 auto;
}

#image {
max-width: 100%;
}
Loading