Skip to content

Commit affa67f

Browse files
committed
Objects Studio
1 parent 640424f commit affa67f

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

objects-and-math/studio/ObjectsStudio01.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
11
// Code your selectRandomEntry function here:
2+
function selectRandomEntry (arr) {
3+
let index = Math.floor(Math.random() * arr.length);
4+
return arr[index];
5+
}
26

7+
function selectThreeRandomIDs () {
8+
let randomlySelectedIDs = [];
9+
10+
for (let i = 0; i < 3; i++) {
11+
let randomEntry = selectRandomEntry(idNumbers);
12+
while (randomlySelectedIDs.includes(randomEntry)) {
13+
randomEntry = selectRandomEntry(idNumbers);
14+
}
15+
randomlySelectedIDs.push(randomEntry);
16+
}
17+
return randomlySelectedIDs;
18+
}
319

420
// Code your buildCrewArray function here:
21+
function buildCrewArray (arrSelectRandomIDs, arrAllCandidates) {
22+
let crew = [];
23+
24+
arrAllCandidates.forEach(element => {
25+
for (let i = 0; i < arrSelectRandomIDs.length; i++) {
26+
if (element.astronautID === arrSelectRandomIDs[i]) {
27+
crew.push(element.astronautID);
28+
} else {
29+
30+
}
31+
}
32+
});
33+
34+
let selectedCrewMsg = `${crew[0]}, ${crew[1]}, and ${crew[2]} are going to space!`;
35+
return crew, selectedCrewMsg;
36+
}
537

638

739
let idNumbers = [291, 414, 503, 599, 796, 890];
@@ -53,3 +85,6 @@ let candidateF = {
5385
let animals = [candidateA,candidateB,candidateC,candidateD,candidateE,candidateF];
5486

5587
// Code your template literal and console.log statements:
88+
89+
//console.log(selectThreeRandomIDs());
90+
console.log(buildCrewArray(selectThreeRandomIDs(), animals));

objects-and-math/studio/ObjectsStudio02.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
// Code your orbitCircumference function here:
2-
2+
const orbitCircumference = function() {
3+
return 2 * (Math.PI) * 2000;
4+
}
35

46
// Code your missionDuration function here:
5-
7+
function missionDuration(numOfOrbits = 5, orbitRadius = 2000, orbitalSpeed = 28000) {
8+
let time = (orbitCircumference() * numOfOrbits) / orbitalSpeed;
9+
time = (Math.round(time * 100) / 100);
10+
let resultMsg = `The mission will travel ${orbitCircumference() * numOfOrbits} km around the planet, and it will take ${time} hours to complete.`
11+
console.log(resultMsg); //? Should I refactor to log message outside the function?
12+
return time;
13+
}
614

715
// Copy/paste your selectRandomEntry function here:
8-
16+
function selectRandomEntry (arr) {
17+
let index = Math.floor(Math.random() * arr.length);
18+
return arr[index];
19+
}
920

1021
// Code your oxygenExpended function here:
11-
22+
function oxygenExpended (candidate, orbitalSpeed = 28000, orbitRadius = 2000) {
23+
let time = missionDuration(3, orbitRadius, orbitalSpeed);
24+
let result = `${candidate.name} will perform the spacewalk, which will last ${time} hours and require ${candidate.o2Used(time)} kg of oxygen.`;
25+
return result;
26+
}
1227

1328
// Candidate data & crew array.
1429
let candidateA = {
@@ -54,5 +69,10 @@ let candidateA = {
5469
'astronautID':890
5570
};
5671

57-
let crew = [candidateA,candidateC,candidateE];
58-
72+
let crew = [candidateA,candidateB,candidateC,candidateD,candidateE,candidateF];
73+
74+
console.log(orbitCircumference());
75+
//onsole.log(missionDuration());
76+
console.log(oxygenExpended(selectRandomEntry(crew)));
77+
78+
//console.log(crew[0]);

objects-and-math/studio/ObjectsStudio03.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
// Code your crewMass function here:
2+
const crewMass = function (selectedCrew) {
3+
let totalCrewMass = 0;
24

5+
for (let i = 0; i < selectedCrew.length; i++) {
6+
totalCrewMass += selectedCrew[i].mass;
7+
}
8+
return Math.round(totalCrewMass * 10) / 10;
9+
};
310

411
// Code your fuelRequired function here:
12+
function fuelRequired (totalMassOfCrew, selectedCrew) {
13+
const massOfRocket = 75000;
14+
const fuelToLiftOnekg = 9.5;
15+
let totalMass = massOfRocket + totalMassOfCrew;
16+
let fuelNeededForLEO = totalMass * fuelToLiftOnekg;
517

18+
for (let i = 0; i < selectedCrew.length; i++) {
19+
if (selectedCrew[i].species === 'cat' || selectedCrew[i].species === 'dog') {
20+
totalMass += 200
21+
} else {
22+
totalMass += 100;
23+
}
24+
}
25+
26+
let result = `The mission has a launch mass of ${Math.ceil(totalMass)} kg and requires ${fuelNeededForLEO} kg of fuel.`;
27+
return result;
28+
29+
}
630

731
// The pre-selected crew is in the array at the end of this file.
832
// Feel free to add, remove, or switch crew members as you see fit.
@@ -51,4 +75,6 @@ let candidateA = {
5175
};
5276

5377
let crew = [candidateB,candidateD,candidateF];
54-
78+
79+
//console.log(crewMass(crew));
80+
console.log(fuelRequired(crewMass(crew), crew));

0 commit comments

Comments
 (0)