Build a Space Mission Roster - Step 20

Tell us what’s happening:

I have used both dot notation and bracket
I have done both a for in loop and the more traditional for loop that is in the code now.
Why do I still get the “you should use a for loop to iterate through the EVAReadySquad array and log the name of each astronaut” complaint?

The output is correct.

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];

  return updatedCrew; 
}

const updatedSquad = swapCrewMembers(squad, 2, 5);

function sortByPriorityDescending(crew) {
  for (let i = 0; i < crew.length - 1; i++) {
    for (let j = 0; j < crew.length - 1 - i; j++) {
      if (crew[j].priority < crew[j + 1].priority) {
        const temp = crew[j];
        crew[j] = crew[j + 1];
        crew[j + 1] = temp;
      }
    }
  }
}

function getEVAReadyCrew(crew) {
  const eligible = [];
  for (const astronaut of crew) {
    if (astronaut.isEVAEligible) eligible.push(astronaut);
  }
  sortByPriorityDescending(eligible); 
  
  return eligible;
}

// User Editable Region
let EVAReadySquad = getEVAReadyCrew(updatedSquad);
for (let i = 0; i < EVAReadySquad.length - 1; i++) {
console.log(EVAReadySquad[i].name);
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:152.0) Gecko/20100101 Firefox/152.0

Challenge Information:

Build a Space Mission Roster - Step 20

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

Welcome to the forum @blatboy,

Try logging the length of EVAReadySquad just above the loop. Is that the number of names you see logged to the console? Check your loop variable.

Happy coding

Thank you. I still don’t know why it still threw me that error when I used the for in approach. When I did that approach I didn’t use length -1. It’s ok tho. It passes now and I’ll just move on and hopefully learn one day.

The reason your condition i < EVAReadySquad.length - 1 wasn’t working is because your index stopped before the last item in the array. Had you said i <= EVAReadySquad.length - 1, the code would have worked.

Edit:
A for..in loop is used to loop over an object.
for…in - JavaScript | MDN

Yes I understood why the - 1 didn’t work. Thank you for clarifying that for..in is only for objects. Appreciated. :folded_hands:. Of course I was thinking of “for..of” which, in theory should have worked.