Skip to content
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

Update script.js #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 15 additions & 17 deletions 24-joke-generator/script.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
const jokeText = document.getElementById('jokeText');
let button = document.getElementById('btn');
let url = "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,explicit&type=single"
const button = document.getElementById('btn');
const url = "https://v2.jokeapi.dev/joke/Any?blacklistFlags=nsfw,religious,political,racist,explicit&type=single";

const getJoke = () => {
return new Promise((resolve, reject) => {
fetch(url)
.then(data => data.json())
.then(item => {
jokeText.textContent = item.joke;
resolve();
})
.catch(error => {
console.error('Error fetching joke:', error);
});
});
}
const getJoke = async () => {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const item = await response.json();
jokeText.textContent = item.joke || "No joke found!";
} catch (error) {
console.error('Error fetching joke:', error);
jokeText.textContent = "Sorry, couldn't fetch a joke. Please try again.";
}
};

button.addEventListener('click', getJoke);