forked from LaunchCodeEducation/javascript-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
76 lines (64 loc) · 2.58 KB
/
Copy pathscripts.js
File metadata and controls
76 lines (64 loc) · 2.58 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*** Random Quote, Advice, & Dad Jokes ***/
/*
Some practice on using fetch - utilizing three different public APIs (linked below)
*/
// Event listener for page load
window.addEventListener('load', function() {
// Create object for result section
const result = document.getElementById('result');
// Click events using document-level event listener (event delegation)
document.addEventListener('click', function (event) {
// TODO: If the random advice button is clicked, put the advice in the result section
// RANDOM QUOTE courtesy of https://api.quotable.io/random
if(event.target.id === "quote") {
fetch ('https://api.quotable.io/random').then(function(response) {
response.json().then(function(data) {
console.log(data);
result.innerHTML = `
<p class = "text-left">"${data.content}"</p>
<p class = "text-right">-${data.author}"</p>
`;
})
});
}
// RANDOM ADVICE courtesy of https://api.adviceslip.com/
// The endpoint is https://api.adviceslip.com/advice
if(event.target.id === "advice") {
fetch ('https://api.adviceslip.com/advice')
.then(function(response) {return response.json()})
.then(function(data){
// console.log(data);
result.innerHTML = `
<p>${data.slip.advice}</p>
`;
});
};
/*
// TODO: If dad joke button is clicked, put dad joke in result section
The endpoint is https://icanhazdadjoke.com
This one also requires a header. Add the following object as the second argument in the fetch() function:
{
headers: {
Accept: "application/json",
}
}
*/
// Challenge! Use async/await syntax for this one.
// RANDOM DAD JOKE courtesy of https://icanhazdadjoke.com/api
if(event.target.id === "dad-joke") {
async function getDadJoke() {
let response = await fetch('https://icanhazdadjoke.com', {
headers: {
Accept: "application/json",
}
});
let data = await response.json();
// console.log(data);
result.innerHTML = `
<p>${data.joke}</p>
`;
}
getDadJoke();
};
});
});