Build a Space Mission Roster - Step 13

Tell us what’s happening:

When I tested logging the results of the below code snippet to the console, the output is “undefined” for the crew names. I’ve been unable to figure out why or how to fix this, even after trying multiple types of “for” loops.

  for (let name of updatedCrew) {
    console.log(`${updatedCrew.name}`);
  };

Your code so far

const squad = [];

const firstAstronaut = {
  id: 1,
  name: "Andy",
  role: "Commander",
  isEVAEligible: true,
  priority: 3
};

function addCrewMember(crew, astronaut) {
  for (let i = 0; i < crew.length; i++) {
    if (crew[i].id === astronaut.id) {
      console.log("Duplicate ID: " + astronaut.id);
      return;
    }
  }
  crew.push(astronaut);
}

addCrewMember(squad, firstAstronaut);

const remainingCrew = [
  { id: 2, name: "Bart", role: "Pilot", isEVAEligible: false, priority: 8 },
  { id: 3, name: "Caroline", role: "Engineer", isEVAEligible: true, priority: 4 },
  { id: 4, name: "Diego", role: "Scientist", isEVAEligible: false, priority: 1 },
  { id: 5, name: "Elise", role: "Medic", isEVAEligible: true, priority: 7 },
  { id: 6, name: "Felix", role: "Navigator", isEVAEligible: true, priority: 6 },
  { id: 7, name: "Gertrude", role: "Communications", isEVAEligible: false, priority: 4 },
  { id: 8, name: "Hank", role: "Mechanic", isEVAEligible: true, priority: 2 },
  { id: 9, name: "Irene", role: "Specialist", isEVAEligible: true, priority: 5 },
  { id: 10, name: "Joan", role: "Technician", isEVAEligible: false, priority: 1 },
];

for (let i = 0; i < remainingCrew.length; i++) {
  addCrewMember(squad, remainingCrew[i]);
}

function swapCrewMembers(crew, fromIndex, toIndex) {
  if (
    fromIndex < 0 || 
    toIndex < 0 ||
    fromIndex >= crew.length ||
    toIndex >= crew.length
  ) {
    console.log("Invalid crew indices");
    return;
  }

  const updatedCrew = crew.slice();
  updatedCrew[fromIndex] = updatedCrew.splice(toIndex, 1, updatedCrew[fromIndex])[0];

// User Editable Region


  for (let name of updatedCrew) {
    console.log(`${updatedCrew.name}`);
  };
  
  return updatedCrew;


// User Editable Region

}

console.log(swapCrewMembers(remainingCrew, 0, 1));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36

Challenge Information:

Build a Space Mission Roster - Step 13

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/workshop-space-mission-roster/694d98a87c2a7a33ca92cfa9.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @snaylspace,

Use a for loop to log the name of every astronaut in the updatedCrew array.

The instructions specifically ask you to use a for loop, not a for...of loop.

Happy coding

Thanks dhess - Other users had had success using a “for… of” which is why I was asking this. Eg.

Yes; I tested it and it does work…when the code inside the body of the loop is correct.

You passed remainingCrew into your swapCrewMembers function call. What type of structure is each item in remainingCrew? So, when you say for(let name of updateCrew), what is name? Is trying to log updatedCrew.name going to print the crew member name? What do you see in the console?

Happy coding

Hi @dhess - When attempting to print the names, the code currently prints “undefined” into the console, for each of the names.

Each item in the remainingCrew array is an object. So, in for(let name of updateCrew) , “name” refers to an object, or perhaps an object’s property value. Given the array nature of a “for … of” loop, my thinking is that logging updatedCrew.name, while looping through the array, would print the “name” property of each array item into the console.

The swapCrewMembers function & call seems to work, based on the console.log outputs:

console.log(swapCrewMembers(remainingCrew, 0, 1));

Thanks for your help!

You might want to change the loop variable name to obj or person to make this easier to conceptualize.

Thank you! That worked. :slight_smile: