Skip to content

Commit c9b37a6

Browse files
committed
ObjectsStudio
1 parent 283f9db commit c9b37a6

File tree

4 files changed

+79
-12
lines changed

4 files changed

+79
-12
lines changed

.vscode/launch.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"skipFiles": [
12+
"<node_internals>/**"
13+
],
14+
"program": "${workspaceFolder}/objects-and-math/chapter-examples/KindnessSelection.js"
15+
}
16+
]
17+
}

objects-and-math/chapter-examples/KindnessSelection.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@ function randomSelection(arr){
77

88
let words = ['Hello', 'World', 'Python', 'JavaScript', 'Rutabaga'];
99

10-
for (i=0; i < 8; i++){
11-
console.log(randomSelection(happiness));
12-
//console.log(randomSelection(words))
13-
}
10+
// for (i=0; i < 8; i++){
11+
// console.log(randomSelection(happiness));
12+
// //console.log(randomSelection(words))
13+
// }
14+
let randomArr = randomSelection([happiness, words]);
1415

16+
for (let i=0; i < 2; i++) {
17+
console.log(randomSelection(randomArr));
18+
}
19+
let newArr = [];
20+
newArr.push(randomSelection(happiness));
21+
newArr.push(randomSelection(words));
22+
console.log(newArr);
1523
//Experiment with the code above. Try to:
1624
//a) Print 3 random selections from each array.
1725
//b) Have the code randomly pick one array, and then print 2 random items from it.

objects-and-math/studio/ObjectsStudio01.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
// Code your selectRandomEntry function here:
2-
2+
function selectRandomEntry(arr){
3+
return
4+
arr[Math.floor(Math.random()*arr.length)];
5+
}
36

47
// Code your buildCrewArray function here:
5-
8+
function buildCrewArray(candidates, ids){
9+
let crew = [];
10+
for(let i=0; i<candidates.length; i++){
11+
for(let j=0; j<ids.length; j++){
12+
if(candidates[i].astronautID === ids[j]){
13+
crew.push(candidates[i]);
14+
}
15+
}
16+
}
17+
return crew;
18+
}
619

720
let idNumbers = [291, 414, 503, 599, 796, 890];
821

@@ -53,3 +66,18 @@ let candidateF = {
5366
let animals = [candidateA,candidateB,candidateC,candidateD,candidateE,candidateF];
5467

5568
// Code your template literal and console.log statements:
69+
let candidates = [selectRandomEntry(animals),
70+
selectRandomEntry(animals),
71+
selectRandomEntry(animals)];
72+
let randomIds = [];
73+
while(randomIds.length < 3){
74+
let id = selectRandomEntry(idNumbers);
75+
if(!randomIds.includes(id)){
76+
randomIds.push(id);
77+
}
78+
}
79+
let crew = buildCrewArray(animals, randomIds);
80+
81+
console.log(`${crew[0].name},
82+
${crew[1].name},
83+
${crew[2].name} are going to space!`);

objects-and-math/studio/ObjectsStudio02.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
// Code your orbitCircumference function here:
2-
3-
2+
function orbitCircumference (radius) {
3+
return Math.round(2*Math.PI*radius)
4+
};
5+
//console.log(orbitCircumference(2000));
46
// Code your missionDuration function here:
5-
6-
7+
function missionDuration (numOrbits, orbitRadius = 2000, orbitalSpeed = 28000) {
8+
let totalKM = numOrbits * orbitCircumference(orbitRadius);
9+
let totalTime = Math.round(((numOrbits * orbitCircumference(orbitRadius))/ orbitalSpeed) * 100)/100;
10+
return totalTime;
11+
//console.log(`The mission will travel ${totalKM} km around the planet, and it will take ${totalTime} hours to complete.`);
12+
}
713
// Copy/paste your selectRandomEntry function here:
8-
14+
function selectRandomEntry(arr){
15+
return arr[Math.floor(Math.random()*arr.length)];
16+
}
917

1018
// Code your oxygenExpended function here:
19+
function oxygenExpended(astro,orbitRadius =2000, orbitalSpeed =28000) {
20+
let walkDur = missionDuration(3,orbitRadius,orbitalSpeed);
21+
let o2 = Math.round((astro.o2Used(walkDur))*1000)/1000;
1122

23+
return `${astro.name} will perform the spacewalk, which will last ${walkDur} hours and require ${o2} kg of oxygen.`
24+
}
1225

1326
// Candidate data & crew array.
1427
let candidateA = {
@@ -55,4 +68,5 @@ let candidateA = {
5568
};
5669

5770
let crew = [candidateA,candidateC,candidateE];
58-
71+
72+
console.log(oxygenExpended(candidateB));

0 commit comments

Comments
 (0)