Chapter 12 Studio: Objects & Math

I am embarrassed to ask more questions to gain clarity about instructions in Unit 1’s materials. I have completed the studio, but I am unsure I formatted my selectRandomEntry function properly. Also, I am not sure the result of my oxygenExpended is correct.

Here’s my approach to the selectRandomEntry problem:

  1. Create a function that selects only one random entry from the idNumbers array.
  2. Outside the function, I initialized an empty array to hold the results of a loop that’s grabbing the astronautId of the selected candidates. I kept this outside the function in attempts to have the function do one thing, but I’m worried I’m wrong about my approach.

My buildCrewArray function finds the animal objects using the array of IDs. ObjectsStudio01 works as it should, but I just wanted to check if my approach to the selectRandomEntry is inefficient.

My oxygenExpended function seems to calculate everything correctly except for the oxygen used. Each candidate seems to consume 0 kg of oxygen. I rounded the calculation of the o2Used method to the three decimal places. I tested it with candidateA, a radius of 2000, an orbital speed of 28000, and 3 orbits completed. Maybe, I’m calculating time wrong? I calculated how much time it would take for one orbit. Then, I multiplied it by the number of orbits completed.

Sounds right to me.

o2Used method has only one parameter: a duration in hours. It can only output 0 if you are passing in 0 as the argument. Try using console.log() as a debugging tool in your functions to see where your calculations may be failing or where an incorrect argument may be getting passed in.

I tested my oxygenExpended function with the following test values for candidateA:

Number of Orbits: 3
Orbit Radius: 2000
Orbital Speed: 28000

My console.log() tests returned these results:

Hours: 1.35
Candidate’s O2 used (not rounded value): 0.04725000000000001; 0 (rounded)

I can’t tell where I’ve passed in an incorrect value yet. Isn’t distance the same as circumference?

So, the oxygen consumption is not really 0. It becomes zero as a result of your rounding.
The requirement was: “Round the answer to 3 decimal places.:wink: That should be 0.047.

You’re right. I just realized my closing parenthesis included the entire division…:disappointed: Thank you for your help.

UPDATE: Were you able to implement the selectRandomEntry function and loops when calling the oxygenExpended function? My code ran without errors a few times, but it would sometimes return a type error.

I did, but it wasn’t straight-forward. Perhaps I missed a chapter which explains a simpler approach.

In the studio, there is a disconnect between what selectRandomEntry returns (an array of integers) - and what oxygenExpended expects as a parameter (a candidate object). So, you have to find the candidate which matches the ID selected by selectRandomEntry. It’s solvable, but they don’t really tell you how to do that in the studio.

Perhaps, this time * I * am overthinking. LOL

It’s probably just me lol. I tried two approaches for selectRandomEntry: one for returning an array of integers, and another returning the selected objects. But, I settled on returning an array of integers instead. I guess this studio is giving us room to experiment.

UPDATE: So far, no luck on implementing the optionalselectRandomEntry function for the second part of the studio. I guess I’ll just move on to the bonus missions or just move onto the next chapter…

It’s hard to explain without showing a code example. Essentially, you need a helper function which takes an ID (which was returned by selectRandomEntry) and finds and returns the object with that ID.

Too bad this board is dead and we are not getting any input from anyone else (or from LaunchCode). :man_shrugging:

Yeah, I did attempt to use a helper function to handle returning the object(s) if their ID(s) match, but it returned type errors.

I noticed the low activity on this board as well. Do you know if LaunchCode is using a new board or Slack more? I’ve visited the LaunchCode community Slack workspace, but there weren’t many discussions about the JavaScript materials.

No clue. I didn’t realize there was a “central” LaunchCode Slack.

I don’t know if this is the problem you are having, but maybe it’ll help: The syntax to access the value of an object’s property within an array of objects is arr[i][key] (where i is the element’s index and key is a string variable which holds a key’s name.

So, if you have:
let candidates = [{
'name':'Gordon Shumway',
'astronautID':414
},
{
'name':'Lassie',
'astronautID':503
}];

then
let i = 1;
let key = 'name';
console.log(candidates[i][key]);

will give you Lassie

Yeah, it’s listed as LaunchCode Community. LaunchCode shared it in an email in the past, I think. I believe this is the link where you can send your email for an invite: https://launchcodecommunity.herokuapp.com/

I forgot that you could grab keys that way. I attempted to do that using a for loop, but it returned an empty array (even though the array did contain the object when I used console.log()). The while loop worked instead.

I’m still worried that my code is too long or weird (esp. the o2Used result), but it’s working. I haven’t been able to solve the “Conserve O2” bonus mission. I’ll try to solve the other one before moving on to Chapter 13. Thank you again for your help!