Challenge 9 Megathread

was gonna suggest that lol great job!

roster.job Is dot notation, used for objects. But roster is an array, and each array item is an object. For direct access, you’d use something like roster[0].job, which is why the hint tells you to look into loops.

2 Likes

Thanks! in this case it looks like there’s only one Astronaut in the roster so it was a “one and done” with simply using roster[0].job without a loop lol

1 Like

True, but did you learn anything? :slight_smile:

I sincerely hope LHL starts to add more complex tests, that will force us to think a little more out of the box.

1 Like

That’s what I used as well to make it easy! I’ve always had a hard time coding with loops but I had a better understanding after this challenge.

2 Likes

That’s just an example. Our function only brings back a length of one Array. Ie just shuttle DJ.

I should’ve done something wrong. I got the Shuttle DJ but with only 1 passing test.

My code:

roster.forEach(function (item) {
console.log(item.job);
});

return roster;

Yeah this threw me for a loop, pun intended, didn’t need a loop for this challenge but going to try to put one in anyways :expressionless:

I personally set the new array outside my loop then used

let jobs = [];
for (let astro of roster) {
    **add jobs here
}

where astro will be like your roster[loop] but you don’t have to declare the index as such.

is expected a fixed length of roster per run. So, no matter how many interactions you’re doing, astronautJobs it will receive the same array position [roster.length-1].

Your function inside the forEach is just console logging the item.job. After that, you’re returning the intact roster itself.

1 Like

This challenge technically didn’t need a loop, but I would definitely use a loop for it since we are working with arrays. Even though it is not needed, it is good practice to access an array dynamically, rather than accessing the data with roster[0]. We won’t always know the length of our array so accessing it dynamically will help us long term. Happy to clear this up if anyone has questions!

Best of luck :smiley:

3 Likes

@Deena, I used a bigger roster and seems that is ok too, there is no fixed answer. The algorithm is adjusted for all possibilities.

const aFarAwayRoster= [
  {
    firstName:"Chris",
    lastName: "Hadfield",
    nickname:"Space Oddity",
    prefix:"Astronaut",
    job:"Shuttle DJ"
  },
  {
    firstName:"Darth",
    lastName: "Vader",
    nickname:"The Chosen One",
    prefix:"Astronaut",
    job:"Party Lights Commander"
  },
  {
    firstName:"Luke",
    lastName: "Skywalker",
    nickname:"Space Even",
    prefix:"Astronaut",
    job:"Space Cook"
  },
  {
    firstName:"Han",
    lastName: "Solo",
    nickname:"ShotFirst",
    prefix:"Astronaut",
    job:"Pilot"
  }
];
3 Likes

Careful what you wish for :wink:

Challenges are built to add complexity gradually, so things might get more “interesting” in the next week

3 Likes

Good thought using array.forEach()!

I would recommend taking a look at array.map() as it may be more appropriate here.

I’ll leave it to you to figure out why :slight_smile:

1 Like

This took me longer than I wanted…
But what I struggled with until I finally understood that I needed to push to a new array.
I created a new array, did the forEach loop, push it on the new array, and then return the new array.
It works, but not sure if it was the best solution.

2 Likes

I was using “in” instead of “of” and learned the difference the hard way! :smiley:

3 Likes

I started out the same. It took longer than expected.
I created a new array, I ended up with a for loop, and then I pushed it to my new array and returned it.

For me though, turns out I had a good loop going quickly if I used the for loop, but the forEach wasn’t working for me. I also tried a while loop as that is very simple to setup but again I abandoned it because I wasn’t getting my array filled. I guess I could have stuck with the while loop after all since my biggest issue was pushing a variable into my array. I was messing with = {} [] () . in all configurations until something finally worked.

1 Like

this ended up taking more time than I thought.
a big hint, look here, specifically “Converting a for loop to forEach”

You can do it either way and it’ll work.

2 Likes

Check your syntax when adding to astronautJobs… that is not the correct way to “push” to an array… HINT HINT

1 Like