forked from LaunchCodeEducation/javascript-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (46 loc) · 1.82 KB
/
Copy pathscript.js
File metadata and controls
53 lines (46 loc) · 1.82 KB
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
49
50
51
52
53
//TODO: Add Your Code Below
function init() {
let url = "https://handlers.education.launchcode.org/static/astronauts.json"
fetch(url)
.then((response) => {
return response.json();
})
.then((data) => {
let astronauts = data;
document.getElementById('totalCrew').innerHTML += astronauts.length;
// Sorts astronauts by time in space before they are added to HTML
astronauts.sort(function(a, b) {
return parseFloat(a.hoursInSpace) - parseFloat(b.hoursInSpace);
});
for (crewMember in astronauts) {
let firstName = astronauts[crewMember].firstName;
let lastName = astronauts[crewMember].lastName;
let hoursInSpace = astronauts[crewMember].hoursInSpace;
let active = astronauts[crewMember].active;
// Changes active status to green
let status;
if (astronauts[crewMember].active === true) {
status = `<li id="active">Active: ${active}</li>`
} else {
status = `<li>Active: ${active}</li>`
}
let skills = astronauts[crewMember].skills.join(", ");
let picture = astronauts[crewMember].picture;
let newCrewMember = `
<div class="astronaut">
<div class="bio">
<h3>${firstName} ${lastName}</h3>
<ul>
<li>Hours in space: ${hoursInSpace}</li>
${status}
<li>Skills: ${skills}</li>
</ul>
</div>
<img class="avatar" src="${picture}">
</div>
`
document.getElementById('container').innerHTML += newCrewMember;
}
})
}
window.addEventListener('load', init);