|  | 
|  | 1 | +<style> | 
|  | 2 | + /* Defines a cleaner look for tables */ | 
|  | 3 | + table { border-collapse: collapse; } | 
|  | 4 | + td, th { border: 1px solid black; padding: 3px 8px; } | 
|  | 5 | + th { text-align: left; } | 
|  | 6 | +</style> | 
|  | 7 | + | 
|  | 8 | +<script> | 
|  | 9 | + function buildTable(data) { | 
|  | 10 | + var table = document.createElement("table"); | 
|  | 11 | + | 
|  | 12 | + var tr1 = document.createElement("tr"); | 
|  | 13 | + Object.keys(data[0]).forEach(function(key) { | 
|  | 14 | + var th = document.createElement("th"); | 
|  | 15 | + var txt = document.createTextNode(key); | 
|  | 16 | + th.appendChild(txt); | 
|  | 17 | + tr1.appendChild(th); | 
|  | 18 | + }); | 
|  | 19 | + table.appendChild(tr1); | 
|  | 20 | + | 
|  | 21 | + data.forEach(function(rowData) { | 
|  | 22 | + var tri = document.createElement("tr"); | 
|  | 23 | + for(var key in rowData) { | 
|  | 24 | + var td = document.createElement("td"); | 
|  | 25 | + var txt = document.createTextNode(rowData[key]); | 
|  | 26 | + if(key == "height") td.style.textAlign = "right"; | 
|  | 27 | + td.appendChild(txt); | 
|  | 28 | + tri.appendChild(td); | 
|  | 29 | + } | 
|  | 30 | + table.appendChild(tri); | 
|  | 31 | + }); | 
|  | 32 | + | 
|  | 33 | + return table; | 
|  | 34 | + } | 
|  | 35 | + | 
|  | 36 | + var MOUNTAINS = [ | 
|  | 37 | + {name: "Kilimanjaro", height: 5895, country: "Tanzania"}, | 
|  | 38 | + {name: "Everest", height: 8848, country: "Nepal"}, | 
|  | 39 | + {name: "Mount Fuji", height: 3776, country: "Japan"}, | 
|  | 40 | + {name: "Mont Blanc", height: 4808, country: "Italy/France"}, | 
|  | 41 | + {name: "Vaalserberg", height: 323, country: "Netherlands"}, | 
|  | 42 | + {name: "Denali", height: 6168, country: "United States"}, | 
|  | 43 | + {name: "Popocatepetl", height: 5465, country: "Mexico"} | 
|  | 44 | + ]; | 
|  | 45 | + | 
|  | 46 | + document.body.appendChild(buildTable(MOUNTAINS)); | 
|  | 47 | +</script> | 
0 commit comments