Skip to content

Commit 1ad6853

Browse files
committed
Complete 26.6.3 Bonuus Missions 1 and 2
1 parent f2f5147 commit 1ad6853

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

script.js

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,33 @@ window.addEventListener("load", function() {
66
.then(function(jsonString) {
77
// console.log(jsonString);
88

9-
let astronauntInfo = "";
9+
// 26.6.3 Bonus Missions
10+
let hoursArray = [];
11+
jsonString.forEach(function(astronaut) {
12+
hoursArray.push(astronaut.hoursInSpace);
13+
});
14+
15+
hoursArray = mergeSort(hoursArray);
16+
17+
let astronautInfo = "";
18+
for (let i = hoursArray.length - 1; i >= 0; --i) {
19+
for (const astronaut of jsonString) {
20+
if (astronaut.hoursInSpace === hoursArray[i]) {
21+
astronautInfo += addHTML(astronaut);
22+
}
23+
}
24+
}
25+
26+
27+
// 26.6.2 Requirements
28+
/*
29+
let astronautInfo = "";
1030
for (let i = 0; i < jsonString.length; i++) {
11-
astronauntInfo += addHTML(jsonString[i]);
31+
astronautInfo += addHTML(jsonString[i]);
1232
}
33+
*/
1334

14-
document.getElementById("container").innerHTML = astronauntInfo;
35+
document.getElementById("container").innerHTML = astronautInfo;
1536
});
1637
});
1738
});
@@ -23,12 +44,48 @@ function addHTML(data) {
2344
<h3>${data.firstName} ${data.lastName}</h3>
2445
<ul>
2546
<li>Hours in space: ${data.hoursInSpace}</li>
26-
<li>Active: ${data.active}</li>
27-
<li>Skills: ${data.skills}</li>
47+
<li style="${data.active ? "color: green;" : ""}">Active: ${data.active}</li>
48+
<li>Skills: ${data.skills.join(", ")}</li>
2849
</ul>
2950
</div>
3051
<img class="avatar" src="${data.picture}">
3152
</div>
3253
`;
3354
return htmlWraper;
55+
}
56+
57+
function mergeSort(arr) {
58+
if (arr.length < 2) {
59+
return arr;
60+
}
61+
let middle = Math.floor(arr.length / 2);
62+
let left = arr.slice(0, middle);
63+
let right = arr.slice(middle);
64+
65+
left = mergeSort(left);
66+
right = mergeSort(right);
67+
68+
return merge(left, right);
69+
}
70+
71+
function merge(arr1, arr2) {
72+
let sortedArr = [];
73+
74+
while (arr1.length > 0 && arr2.length > 0) {
75+
if (arr1[0] > arr2[0]) {
76+
sortedArr.push(arr2.shift());
77+
} else {
78+
sortedArr.push(arr1.shift());
79+
}
80+
}
81+
82+
while (arr1.length > 0) {
83+
sortedArr.push(arr1.shift());
84+
}
85+
86+
while (arr2.length > 0) {
87+
sortedArr.push(arr2.shift());
88+
}
89+
90+
return sortedArr;
3491
}

0 commit comments

Comments
 (0)