forked from CodecoolGlobal/api-wars-python-kamil-g-cc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
28 lines (24 loc) · 919 Bytes
/
app.js
File metadata and controls
28 lines (24 loc) · 919 Bytes
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
let xhr = new XMLHttpRequest();
const tbody = document.getElementById("tbody");
function onReady(){
if(this.readyState != 4) return
let results = JSON.parse(xhr.responseText);
results.results.forEach((el)=>{
let newRow = createTr(el);
tbody.appendChild(newRow);
})
}
function createTr(el){
let newRow = document.createElement("tr");
newRow.innerHTML = `<td>${el.name}</td>\
<td>${new Intl.NumberFormat("en-US").format(el.diameter)} km</td>\
<td>${el.climate}</td>\
<td>${el.terrain}</td>\
<td>${(el.surface_water!="unknown"?el.surface_water+"%":"unknown")}</td>\
<td>${(el.population!="unknown"?"".concat(new Intl.NumberFormat("en-US").format(el.population)):"unknown").concat(" people")}</td>\
`;
return newRow;
}
xhr.onreadystatechange = onReady;
xhr.open("GET", "https://swapi.dev/api/planets");
xhr.send()