Build a Space Mission Roster - Step 12

Tell us what’s happening:

My code doesn’t seem to be correct although I’ve written the correct thing. Any hints?

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();

// User Editable Region

  updatedCrew[1] = updatedCrew.splice(fromIndex, toIndex, updatedCrew[1])[0];
  console.log(updatedCrew);

// User Editable Region

}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36

Challenge Information:

Build a Space Mission Roster - Step 12

hello and welcome to fcc forum :slight_smile:

you need to correctly make use of “fromIndex/toIndex/add or remove” values of “splice” method

checkout this thread and try to get an understanding of it better Build a Space Mission Roster - Step 13

happy coding :slight_smile:

1 Like

Oh, thanks. I tried looking for this but the forum didn’t show me this exact problem.

but it solved this problem you are having here… happy coding :slight_smile:

1 Like

Really stuck and off with this step 12:
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[1] = updatedCrew.splice(fromIndex, toIndex, updatedCrew[1])[0];

return updatedCrew;

}