-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
104 lines (77 loc) · 2.94 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//Final JavaScript
const getFacts = async function () {
// making a network request = asking for resources outside of your reach, so there's no telling how long until you get the stuff back
// browser asks flask server for stuff
const result = await fetch("http://127.0.0.1:5000/facts") // returns the """array"""
console.log(result)
return await result.json() // takes the response object, reads it, and converts the body into json- smn javascript can use
}
const quoteToHTML = function(country) {
// quote obj should have the fields quote and author
// TODO: turn quote obj into some html container
//factTable = document.getElementById("fact-table");
// for (let i = 0; i < quote.length; i++)
// {
// let li = document.createElement("li");
// li.innerText = item;
// list.appendChild(li);
// }
return `
<div class="container">
<div class="table-responsive-lg">
<table class="table table-light">
<tbody>
<tr>
<td>
${country.factlist[0]}
</td>
</tr>
<tr>
<td>
${country.factlist[1]}
</td>
</tr>
<tr>
<td>
${country.factlist[2]}
</td>
</tr>
</tbody>
</table>
</div>
</div>
`
//return "<div>"+quote.fact+"</div>";
}
const generateQuote = async function() {
// javascript rep of the div in html, doc is the DOM
const factTable = document.getElementById('fact-table')
// a check for whether the div exists
if (factTable)
{
factTable.innerHTML = "<h1> Hello! </h1>"
}
console.log({factTable})
const countries = [
{
factlist : ["Fact1","Fact2","Fact3"] ,
author: "Albus Dumbledore",
fact: "Did you know, bacon is tasty?"
},
];
factTable.innerHTML = quoteToHTML(countries[0])
const fact = await getFacts() // invoking getFacts
console.log(fact) // print result
return
let arrayIndex = Math.floor(Math.random() * quotes.length);
document.getElementById("quotes").innerHTML = quotes[arrayIndex].quote;
document.getElementById("author").innerHTML = quotes[arrayIndex].author;
document.getElementById("fact").innerHTML = quotes[arrayIndex].fact;
//Syntax : arrayVariableName[index]
console.log(quotes[3].quote); // If you want to know what a man’s like, take a good look at how he treats his inferiors, not his equals.
console.log(quotes[3].author); // Sirius Black
}
window.onload = function() {
generateQuote();
//document.getElementById("generate").addEventListener('click', generateQuote);
}