Created
April 9, 2019 10:13
-
-
Save nailkhasipov/8ab695f1ca0d6a8552f4713350f3083f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<meta charset="utf8"> | |
<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"} | |
]; | |
function buildTable(data) { | |
let table = document.createElement("table"); | |
let fields = Object.keys(data[0]); | |
let headRow = document.createElement("tr"); | |
fields.forEach(function(field) { | |
let headCell = document.createElement("th"); | |
headCell.textContent = field; | |
headRow.appendChild(headCell); | |
}); | |
table.appendChild(headRow); | |
data.forEach(function(object) { | |
let row = document.createElement("tr"); | |
fields.forEach(function(field) { | |
let cell = document.createElement("td"); | |
cell.textContent = object[field]; | |
if (typeof object[field] == "number") { | |
cell.style.textAlign = "right"; | |
} | |
row.appendChild(cell); | |
}); | |
table.appendChild(row); | |
}); | |
return table; | |
} | |
document.querySelector("#mountains") | |
.appendChild(buildTable(MOUNTAINS)); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment