-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
64 lines (57 loc) · 1.66 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Api Call</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- bootstrap css -->
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
/>
</head>
<body>
<div class="container">
<h2>Table of Advice</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Date</th>
<th>Advice</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script>
const fetchData = async () => {
// fetch raw data
const data = await fetch(
"https://api.adviceslip.com/advice/search/life"
);
// parse body and return array of slips
const rawData = await data.json();
return rawData.slips;
};
const renderData = async () => {
try {
var slips = await fetchData();
// map over array of slips and create table rows accordingly
for (let i = 0; i < slips.length; i++) {
var trTag = document.createElement("TR");
var dateTag = document.createElement("TD");
var adviceTag = document.createElement("TD");
dateTag.textContent = slips[i].date;
adviceTag.textContent = slips[i].advice;
trTag.appendChild(dateTag);
trTag.appendChild(adviceTag);
document.querySelector("tbody").appendChild(trTag);
}
} catch (err) {
alert(err.message);
}
};
renderData();
</script>
</body>
</html>