Challenge 9 Megathread

Welcome to challenge #9!

Use this thread for any and all questions relating to challenge 9

2 Likes

Why the result of the code “console.log(roster.length);” is 1 ? and suppose it is 2 as the exampleRoster has 2 items .

1 Like

I run the code successfully, but with only one item with the job “Shuttle DJ”.
I don’t know if it is correct and I can submit it or not.

It just started working for me.

Same thing with me, only one item and the code is working without errors, I am confused.

Tried a few different loop types, some don’t seem to be working right.

I think the undefined in the console log is just the same issue I’ve seen before with it only console logging the final state of the var.

Today is crystal clear:

" …an array representing the roster of astronauts…"

The problem is not with the loop. I console the roster.length before starting the loop and the result is 1

A few general comments…

  1. An array can have a single value in it [ ‘Hello World’ ] is a valid array of strings which would have a length of 1

  2. the Example is an example and it shouldn’t be assumed that it’s the values being used in the tests being ran against your code.

However we did find an issue where we have valid console output right before the return and it’s not accepting it, so there is something weird/broken with how it’s handling the JS code execution and tests.

1 Like

This is my test result
image
The first is the length of the given array,
the second is the result of the loop.

and there are no errors

2 Likes

The output should be
image

Yeah, it doesn’t matter where you put your console.log, something in the way the challenge page is programmed, it only shows you the final state.

I saw this in an earlier challenge, someone had suggested some method to capture info into a string to put it immutably into the console.

I didn’t figure out what’s working until I made myself a dummy data set, and manipulated that, so I knew exactly how the page was interacting with the data.

const arrRoster = [
  {
    firstName:"Chris",
    lastName: "Hadfield",
    nickname:"Space Oddity",
    prefix:"Astronaut",
    job:"Shuttle DJ"
  },
  {
    firstName:"Guy",
    lastName: "Fieri",
    nickname:"The Flavour",
    prefix:"Astronaut",
    job:"Space Cook"
  }  
] 

My final code is 8 lines total.

Another single line answer for this one. I think the hint might be good for beginners, but I’d probably recommend they rather look into array.map() rather than a loop.

7 Likes

The test only has one item, so don’t sweat it :grinning_face_with_smiling_eyes:

hi guys, feel like this should be simple but. I can’t seem to create an array with the strings in it.


Can’t figure out what I am doing wrong

Hi Joe,

Check how you have setup your for loop, it should follow this layout:

for ([initialExpression]; [conditionExpression]; [incrementExpression])
statement

So something like
for (let i = 0; i < roster.length; i++) {
[whatever you want to have happen in the for loop]
}

Notice when you setup the for loop you need to declare the variable you are using to keep track of your looping through the loop, so I’d suggest using ‘let’, so in your case it would be ‘let loop = [whatever you set it to initially]’

Hope this helps a bit!

PS - You should also declare the astronautJobs variable as an array outside of the for loop before pushing items into it, I’d set it as an empty array. So again declare the variable with let or const; I used let, but technically I think you can use const for this array. I believe I had to return a value also for code to pass the test once the array of strings was created properly.

Nice one! I had forgotten about the array.map() method (it’s been a minute since I’e programmed), thank you for the reminder! It’s definitely a cleaner solution and in larger programs I think the main advantage is maintaining immutability as well as readability (correct me if I am wrong).

In terms of beginners though I think you are right that understanding the concept of a loop is the first thing to learn.

The array.forEach() method is what I found useful for this challenge. There probably are more elegant ways of solving the challenge.

Loop over an Array

fruits.forEach(function(item, index, array) {
  console.log(item, index)
})
// Apple 0
// Banana 1
3 Likes

Why does " console.log(roster.job); " return null?

ah, I needed to use console.log(roster[0].job);

2 Likes