Back to Freecodecamp

Step 5

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

latest2.0 KB
Original Source

--description--

Push the astronaut object into the crew array and log Added ${astronaut.name} as ${astronaut.role} to the console. You can use either template literals or string concatenation for the log message.

--hints--

You should push the astronaut into the crew array.

js
const crew = [];
const astronauts = [
  { id: 1, name: "Abraham", role: "Commander", isEVAEligible: true, priority: 1 },
  { id: 2, name: "Boris", role: "Pilot", isEVAEligible: false, priority: 2 },
  { id: 3, name: "Catarina", role: "Engineer", isEVAEligible: true, priority: 3 }
];

astronauts.forEach(astro => addCrewMember(crew, astro));

assert.strictEqual(crew.length, astronauts.length);
astronauts.forEach((astro, idx) => {
  assert.deepEqual(crew[idx], astro);
});

You should log the astronaut's name in the following format, Added {astronaut.name} as {astronaut.role}.

js
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 crewForLogging = [];
const astronautsToLog = [
  { id: 1, name: "Abraham", role: "Commander" },
  { id: 2, name: "Boris", role: "Pilot" },
  { id: 3, name: "Catarina", role: "Engineer" }
];

const logs = captureConsoleLogs(() => {
  astronautsToLog.forEach(astro => addCrewMember(crewForLogging, astro));
});

assert.strictEqual(logs.length, astronautsToLog.length);

astronautsToLog.forEach((astro, idx) => {
  assert.match(
    logs[idx],
    new RegExp(`Added\\s*${astro.name}\\s*as\\s*${astro.role}`, 'i'),
  );
});

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

--fcc-editable-region--
}