forked from LaunchCodeEducation/Fetch-and-JSON-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (51 loc) · 2.16 KB
/
script.js
File metadata and controls
60 lines (51 loc) · 2.16 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
54
55
56
57
58
59
60
// TODO: add code here
window.addEventListener("load", function () {
// Fetch astronauts data
fetch("https://handlers.education.launchcode.org/static/astronauts.json")
.then(function (response) {
return response.json();
})
.then(function (astronauts) {
// Sort astronauts from most to least time in space
astronauts.sort(function (a, b) {
return b.hoursInSpace - a.hoursInSpace;
});
// Get the container element
const container = document.getElementById("container");
// Loop through each astronaut
astronauts.forEach(function (astronaut) {
// Create HTML elements for each astronaut
const astronautDiv = document.createElement("div");
astronautDiv.className = "astronaut";
const bioDiv = document.createElement("div");
bioDiv.className = "bio";
const h3 = document.createElement("h3");
h3.textContent = astronaut.firstName + " " + astronaut.lastName;
const ul = document.createElement("ul");
const hoursLi = document.createElement("li");
hoursLi.textContent = "Hours in space: " + astronaut.hoursInSpace;
const activeLi = document.createElement("li");
activeLi.textContent = "Active: " + astronaut.active;
if (astronaut.active) {
activeLi.classList.add("active");
}
const skillsLi = document.createElement("li");
skillsLi.textContent = "Skills: " + astronaut.skills.join(", ");
// Append elements to the container div
container.appendChild(astronautDiv);
astronautDiv.appendChild(bioDiv);
bioDiv.appendChild(h3);
bioDiv.appendChild(ul);
ul.appendChild(hoursLi);
ul.appendChild(activeLi);
ul.appendChild(skillsLi);
const avatarImg = document.createElement("img");
avatarImg.className = "avatar";
avatarImg.src = "images/" + astronaut.picture;
astronautDiv.appendChild(avatarImg);
});
})
.catch(function (error) {
console.error("Error fetching astronauts data:", error);
});
});