-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
46 lines (40 loc) · 1.47 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Function to fetch a joke from Official JokeAPI
function fetchJokeFromOfficial() {
fetch('https://official-joke-api.appspot.com/jokes/programming/random')
.then(response => response.json())
.then(jokeData => {
const jokeText = jokeData[0].setup + " " + jokeData[0].punchline;
const jokeElement = document.getElementById('jokeElement');
jokeElement.innerHTML = jokeText + ' 😄';
})
.catch(error => {
console.log('Error:', error);
});
}
// Function to fetch a joke from JokeAPI.io
function fetchJokeFromJokeAPI() {
fetch('https://v2.jokeapi.dev/joke/Programming?type=single')
.then(response => response.json())
.then(jokeData => {
const jokeText = jokeData.joke;
const jokeElement = document.getElementById('jokeElement');
jokeElement.innerHTML = jokeText + ' 😅';
})
.catch(error => {
console.log('Error:', error);
});
}
// Function to randomly choose an API and fetch a joke
function fetchRandomJoke() {
const randomAPI = Math.random() < 0.5 ? 'official' : 'jokeapi';
if (randomAPI === 'official') {
fetchJokeFromOfficial();
} else {
fetchJokeFromJokeAPI();
}
}
// Event listener for the button click
const changeJokeButton = document.getElementById('changeJokeButton');
changeJokeButton.addEventListener('click', fetchRandomJoke);
// Fetch a joke when the page loads
fetchRandomJoke();