Back to Freecodecamp

Step 13

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

latest3.3 KB
Original Source

--description--

Use a for loop to log the name of every astronaut in the updatedCrew array. After the loop, return the updatedCrew array to complete your swapCrewMembers function.

--hints--

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

js
const cleaned = __helpers.removeWhiteSpace(__helpers.removeJSComments(swapCrewMembers.toString())); 
const loopAndLogRegex = /(?:for\([^)]*updatedCrew[^)]*\)[\s\S]*?console\.log\(|_createForOfIteratorHelper\(updatedCrew\)[\s\S]*?console\.log\()/;
assert.match( cleaned, loopAndLogRegex, 'A for(...) loop over updatedCrew is required and console.log() must be inside the loop' );

function captureConsoleLogs(fn) {
  const originalLog = console.log;
  const logs = [];

  console.log = function (...args) {
    logs.push(args.join(" "));
  };

  try {
    fn();
  } finally {
    console.log = originalLog;
  }

  return logs;
}

const sample = [
  { name: "A" },
  { name: "B" },
  { name: "C" }
];

const logs = captureConsoleLogs(() =>
  swapCrewMembers(sample, 0, 2)
);

const expectedOrder = ["C", "B", "A"];

assert.strictEqual(
  logs.length,
  expectedOrder.length,
  "You must log each astronaut's name exactly once"
);

assert.deepEqual(
  logs,
  expectedOrder,
  "console.log must log each astronaut's .name in order"
);

You should return the updatedCrew array after logging its astronauts' names.

js
const sample = [
  { name: "A" },
  { name: "B" },
  { name: "C" }
];
const result = swapCrewMembers(sample, 0, 2);
assert.isArray(result);
assert.deepEqual(result.map(a => a.name), ["C", "B", "A"]);

--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];
--fcc-editable-region--

--fcc-editable-region--
}