-
Notifications
You must be signed in to change notification settings - Fork 1
/
scripts.js
36 lines (34 loc) · 1.13 KB
/
scripts.js
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
/**
* Created by jmccommas on 12/12/17 7:57 PM*/
function createNode(element) {
return document.createElement(element);
}
function append(parent, el) {
return parent.appendChild(el);
}
/// created a fetch function to map data
const ul = document.getElementById('flights');
const url = 'https://api.spacexdata.com/v2/launches';
fetch(url)
.then((resp) => resp.json())
.then(function (data) {
//Received all data
let flights = data;
//Creating parent <ul> element
let ul = createNode('ul');
flights.map(function (flight, index) {
let li = createNode('li'), //Creating <li> element
img = createNode('img'), //Creating <img> element to show mission_path
span = createNode('span'); //Creating <span> element to show flight_number - launch_year
span.innerHTML = `Flight #: ${flight.flight_number} - ${flight.launch_year}`;
img.src= flight.links.mission_patch;
append(li, img);
append(li, span);
append(ul, li);
});
//Finally attaching <ul> list to body
document.body.appendChild(ul);
})
.catch(function (error) {
console.log(JSON.stringify(error));
});