Back to Freecodecamp

Step 17

curriculum/challenges/english/blocks/workshop-space-mission-roster/694e0c6e2cf8b9b45755f0f5.md

latest3.4 KB
Original Source

--description--

First, create an empty array named eligible. Then loop through every astronaut in the input crew array and, if they are EVA-eligible (meaning their isEVAEligible property is true), push them into the eligible array. After the loop, return the eligible array.

--hints--

You should create an array named eligible.

js
const funcStr = __helpers.removeJSComments(getEVAReadyCrew.toString());
assert.match(
  funcStr,
  /(var|let|const)\s+eligible/,
  "You must have at least one space between the declaration keyword (let/const) and 'eligible'"
);
const cleaned = __helpers.removeWhiteSpace(__helpers.removeJSComments(getEVAReadyCrew.toString()));
assert.match(cleaned, /(?:^|[^a-zA-Z])(var|let|const)eligible=\[\]/);

You should use a for loop to iterate through the input crew array.

js
const cleaned = __helpers.removeWhiteSpace(__helpers.removeJSComments(getEVAReadyCrew.toString()));
assert.match(cleaned, /for\s*\(/);

You should return the eligible array.

js
const cleaned = __helpers.removeWhiteSpace(__helpers.removeJSComments(getEVAReadyCrew.toString()));
assert.match(cleaned, /returneligible;?}$/);

const sample2 = [{ isEVAEligible: true }];
assert.isArray(getEVAReadyCrew(sample2));

After calling getEVAReadyCrew(), the returned array should contain only EVA-eligible astronauts from the input array crew.

js
const sample = [
  { name: "A", isEVAEligible: true },
  { name: "B", isEVAEligible: false },
  { name: "C", isEVAEligible: true }
];
const result = getEVAReadyCrew(sample);
assert.deepEqual(result.map(a => a.name), ["A", "C"]);

--seed--

--seed-contents--

js
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 getEVAReadyCrew(crew) {
--fcc-editable-region--

--fcc-editable-region--
}