I completed the exercises, but the instructions for adding the
astronautID property and printing the crew fitness results confuse me.
126.96.36.199. Add a New Property
For each animal, add a property called
astronautID should be assigned a number between 1 and 10 (including 10). However, no crew members should have the same ID.
Initially, I manually assigned each object an ID (ex. 2, 4, 6, 8, 10). But, the instructions stated that IDs should include 10 and not repeat. I created functions to generate a random number between 1 and 10 for each animal. I included conditionals to prevent duplicate IDs from being created. One of my solutions generated a TypeError at the end of the iteration. The other duplicated some of the IDs.
I’ve studied different approaches to creating unique IDs. I tried to apply some concepts to my code, but nothing has worked so far. Are we generating random IDs or manually assigning IDs for objects?
Return the array from the function, then print the results to the console (one animal per line).
With the crew report, I’ve been able to implement the move method. I am unsure if I’m printing it correctly. Right now, it prints the string with all the correct information for each iteration and returns the array.
Is it printed in the wrong order because it’s printing the moves first and the array second?
You are overthinking it again. I think they are teaching you small steps.
They are expecting you to just hardcode the
astronautID property into each object and manually assign an ID, making sure you don’t (manually) assign a duplicate one. If you look at the starter code, the comment at Line 16 seems to guide you toward this approach:
// After you have created the other object literals, add the astronautID property to each one.
Is your second screenshot an answer for 12.7.2. Part 2: Crew Reports and 12.7.3. Part 3: Crew Fitness? That seems wrong. What it should show is the output of your
Thank you for your feedback! The ID range suggestion made me concerned that I needed to implement a function that randomly generates unique IDs.
The second screenshot represents the output of the
fitnessTest function only.
Here’s the output of my
For 12.7.3 Part 3: Crew Fitness, I thought we were to return the array and the result of each animal’s moves. But, now, I’m not sure if I’m supposed to only return the “winner’s” results or the array of turns.
crewReports output looks good. But
fitnessTest, should return an array of strings, each formatted like
'____ took ____ turns to take 20 steps.' That’s how I am reading it. Then you take this output (the array of 5 strings), and print it to the console. If that’s how you are doing it, you are all good. You don’t need to output an array of objects.
The instructions say we should print the results to the console (one animal per line). I was able to do that with
console.log() within the loop. But, would I return the arrays of strings first and use console.log() in a loop outside the function? Or should I print them without using a return statement?
The way I read it is:
The function returns an array of strings;
You call the function assigning its output to a variable;
You loop through the items of your array variable;
Within the loop, you print each item using
Remember, functions are for separation of concerns and code reuse (among other things). There is an idea that a function should do only one thing. Doing
console.log() inside the function causes it to do two things:
- Print to the console
- Return a value
If you wanted to maximize the reuse of your function, perhaps in/by other systems with no UI, you wouldn’t want it to print. There might just not be device to print to.
Ultimately, I am not saying that you are doing it wrong. But there just may be a more optimal solution. I hope it makes sense.
(A sidebar: You could try using the
map method instead of an explicit loop (even if only for practice)).
Thank you for your feedback. The bullet points helped me refactor my function and print it correctly. When I implemented the
map method, it returned an array of empty arrays. I followed the format covered in Chapter 11.3. I may revisit this later to figure out what I did wrong.