Skip to content

Commit fa77b46

Browse files
committed
completed fetch studio
1 parent b66daeb commit fa77b46

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

fetch/studio/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
<body>
1010
<script src="script.js"></script>
1111
<h1>Astronauts</h1>
12+
13+
<!-- Bonus mission 3 -->
14+
<div id="count"></div>
15+
1216
<div id="container">
1317
<!-- List of astronauts will be added here dynamically -->
1418
</div>

fetch/studio/script.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,45 @@
11
//TODO: Add Your Code Below
2+
window.addEventListener("load", function () {
3+
// TODO; create object for container element
4+
const container = document.getElementById("container");
5+
6+
// Fetch using traditional syntax
7+
fetch(
8+
"https://handlers.education.launchcode.org/static/astronauts.json"
9+
).then(function (response) {
10+
response.json().then(function (data) {
11+
console.log(data);
12+
13+
// Bonus mission 1
14+
data.sort(function (a, b) {
15+
return a.hoursInSpace < b.hoursInSpace ? 1 : -1;
16+
});
17+
18+
// Bonus mission 3
19+
const count = document.getElementById('count');
20+
count.innerHTML = `These ${data.length} people have ventured into outer space.`
21+
22+
for (let i = 0; 0 < data.length; i++) {
23+
let astronaut = data[i];
24+
25+
// Bonus mission 2
26+
let activeClass = astronaut.active ? "active" : "";
27+
28+
29+
container.innerHTML += `
30+
<div class = "astronaut">
31+
<div class = "bio">
32+
<h3>${astronaut.firstName} ${astronaut.lastName}</h3>
33+
<ul>
34+
<li>Hours in space: ${astronaut.hoursInSpace}</li>
35+
<li class = ${activeClass}>Active: ${astronaut.active}</li>
36+
<li>Skills: ${astronaut.skills.join(", ")}</li>
37+
</ul>
38+
</div>
39+
<img class = "avatar" src = "${astronaut.picture}"
40+
</div>
41+
`;
42+
}
43+
});
44+
});
45+
});

fetch/studio/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414
margin-bottom: 20px;
1515
border-radius: 6px;
1616
}
17+
18+
.active {
19+
color: green;
20+
}

0 commit comments

Comments
 (0)