-
Notifications
You must be signed in to change notification settings - Fork 2
/
table_DOM.html
48 lines (41 loc) · 1.46 KB
/
table_DOM.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
<!doctype html>
<h1>Mountains</h1>
<div id="mountains"></div>
<script>
const MOUNTAINS = [
{name: "Kilimanjaro", height: 5895, place: "Tanzania"},
{name: "Everest", height: 8848, place: "Nepal"},
{name: "Mount Fuji", height: 3776, place: "Japan"},
{name: "Vaalserberg", height: 323, place: "Netherlands"},
{name: "Denali", height: 6168, place: "United States"},
{name: "Popocatepetl", height: 5465, place: "Mexico"},
{name: "Mont Blanc", height: 4808, place: "Italy/France"}
];
// Your code here
function buildTable (data) {
let table = document.createElement('table');
for (var i = 0; i < data.length; i++) {
let tr = document.createElement('tr');
if (i === 0) {
let headerRow = document.createElement('tr');
for (let col of Object.keys(data[i])) {
let th = document.createElement('th');
let colText = document.createTextNode(String(col).toUpperCase());
th.appendChild(colText);
headerRow.appendChild(th);
}
table.appendChild(headerRow);
}
for (let value of Object.values(data[i])) {
let td = document.createElement('td');
td.appendChild(document.createTextNode(value));
if (+value) td.style.textAlign = "right";
tr.appendChild(td);
}
table.appendChild(tr);
}
return table;
}
let divTable = document.querySelector('#mountains');
divTable.appendChild(buildTable(MOUNTAINS));
</script>