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
45 changes: 0 additions & 45 deletions Assignment_Frontend_2.md

This file was deleted.

21 changes: 21 additions & 0 deletions funny_bones/fetch_jokes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
async function fetchJoke() {
try {
const response = await fetch("https://official-joke-api.appspot.com/random_joke");

if (!response.ok) {
throw new Error(
`Oops! ${response.status} (${response.statusText})`
);
}
const data = await response.json();
Copy link
Contributor

Choose a reason for hiding this comment

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

check for unexpected data formats, here we expect strings

Suggested change
const data = await response.json();
if (typeof data?.setup !== "string" || typeof data?.punchline !== "string") {
throw new Error("Unexpected data format from API");
}

Copy link

Choose a reason for hiding this comment

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

fair point, but this level of validation or defensive programming is not necessary for learning goals at this point

console.log("Here's your joke:")
console.log(data.setup, data.punchline);
}
catch (error) {
console.log("This is embarrassing for us...")
console.error(error.message)
}
}

fetchJoke();

13 changes: 13 additions & 0 deletions funny_bones/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "funny_bones",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs"
}