-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
35 lines (30 loc) · 1.25 KB
/
app.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
document.getElementById('get-jokes').addEventListener('click', getJokes);
let timer = new Date();
function getJokes(e) {
e.target.innerText = 'Generating..';
const time = performance.now(),
jokes = document.getElementById('jokes'),
number = document.getElementById('number').value,
stat = document.getElementById('stat');
stat.value = '';
document.getElementById('number').value = '';
if(isNaN(parseInt(number))) {
stat.innerText = 'Chuck Norris: Enter a number!';
e.target.innerText = 'Get Jokes!';
} else {
fetch(`https://api.icndb.com/jokes/random/${number}`).then(res => res.json()).then(data => {
const fetchedTime = performance.now();
let result = '';
data.value.forEach(joke => {
result += `<li>${joke.joke}</li>`;
});
stat.innerText = `Generated ${number} jokes in ${Math.round(fetchedTime - time)} ms.`;
jokes.innerHTML = result;
e.target.innerText = 'Get Jokes!';
}).catch(err => {
stat.innerText = 'Chuck Norris: Something went wrong, try again!';
e.target.innerText = 'Get Jokes!';
});
}
e.preventDefault();
}