Back to Freecodecamp

Step 20

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

latest4.2 KB
Original Source

--description--

Invoke getEVAReadyCrew() with your updatedSquad roster and store the result in a new variable named EVAReadySquad. Then, use a for loop to log the name of every astronaut in the EVAReadySquad array.

--hints--

You should call getEVAReadyCrew() with updatedSquad and store the result in a new variable named EVAReadySquad.

js
assert.match(
  __helpers.removeJSComments(code),
  /(let|const)\s+EVAReadySquad/,
  "You must have at least one space between the declaration keyword (let/const) and 'EVAReadySquad'"
);
const cleaned = __helpers.removeWhiteSpace(__helpers.removeJSComments(code));
assert.match(cleaned, /(let|const)EVAReadySquad=getEVAReadyCrew\(updatedSquad\)/);
assert.isArray(EVAReadySquad);
assert.strictEqual(EVAReadySquad.length, 6)
const EVANames = EVAReadySquad.map(a => a.name);
assert.deepEqual(EVANames.slice(), ["Elise", "Felix", "Irene", "Caroline", "Andy", "Hank"]);

You should use a for loop to iterate through the EVAReadySquad array and log the name of each astronaut.

js
const cleaned = __helpers.removeJSComments(code).replace(/\s+/g, "");
assert.match(cleaned, /for\([^)]*EVAReadySquad[^)]*\)/, 'A for(...) loop over EVAReadySquad is required');
assert.match(cleaned, /console\.log[^;]*\.name/, 'console.log should log the .name property');
function captureConsoleFromCode(code) {
  const logs = [];
  const originalLog = console.log;
  console.log = (...args) => logs.push(args.join(" "));
  try {
    const userScriptFn = new Function(code);
    userScriptFn();
  } finally {
    console.log = originalLog;
  }
  return logs;
}
const logs = captureConsoleFromCode(code);
const expectedNames = ["Elise", "Felix", "Irene", "Caroline", "Andy", "Hank"];
assert.strictEqual(
  logs.length,
  expectedNames.length,
  "You must log each EVA-ready astronaut's name exactly once"
);
assert.deepEqual(
  logs,
  expectedNames,
  "console.log must log the .name property of each EVA-ready astronaut in order"
);

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

--fcc-editable-region--

--fcc-editable-region--