-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
70 lines (65 loc) · 2.04 KB
/
api.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function handleClick(event){
debugger;
const promise = fetch("https://opentdb.com/api.php?amount=1");
promise.then(parseResponse);
}
function parseResponse(resolveValue){
const response = JSON.parse(resolveValue);
const message = response.message;
display(message);
}
//TEXT METHOD
function parseResponse(resolveValue){
//THE RESOLVE VALUES WILL BE A FETCH OBJECT
//THE TEXT METHOD EXTRACTS THE STRINGIFIED OBJECT
const promise = resolveValue.text();
promise.then(viewResponse);
}
//PARSE METHOD
function viewResponse(resolveValue){
//THE RESOLVE VALUE WILL BE A STRINGIFIED OBJECT
//THE PARSE METHOD GENERATES AN OBJECT FROM THE STRING
const response = JSON.parse(resolveValue);
const results = response.results;
const item = results[0];
const question = item.question;
const answer = item.correct_answer;
const incorrects = item.incorrect_answers;
}
//TRIVIA GAME TEMPLATE
function viewResponse(resolveValue){
//THE RESOLVE VALUE WILL BE A STRINGIFIED OBJECT
//THE PARSE METHOD GENERATES AN OBJECT FROM THE STRING
const response = JSON.parse(resolveValue);
const results = response.results;
const item = results[0];
const question = item.question;
const answer = item.correct_answer;
const incorrects = item.incorrect_answers;
const trivia = `<div>${question}</div>
<ol type="a">
<li>${incorrects[0]}</li>
<li>${incorrects[1]}</li>
<li>${incorrects[2]}</li>
<li>${answer}</li>
</ol>`;
output(trivia);
}
function handleQuoteOfTheDay(){
debugger;
const proxy = "https://thingproxy.freeboard.io/fetch/";
const baseURL = "https://favqs.com/api";
const endPoint = "/qotd";
const URL = (proxy + baseURL + endPoint);
const promise = fetch(URL);
promise.then(extractResponse);
}
//creates stringified objects
function extractResponse(resolveValue){
const promise = resolveValue.text();
promise.then(parseQotdResponse);
}
function parseQotdResponse(resolveValue){
debugger;
const response = JSON.parse();
}