Back to Freecodecamp

Step 10

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

latest4.5 KB
Original Source

--description--

As before, start by validating the input inside the swapCrewMembers function. Specifically, you must validate the indices of the astronauts to be swapped.

If any of the following are true:

  • fromIndex is negative

  • fromIndex is greater than or equal to crew.length

  • toIndex is negative

  • toIndex is greater than or equal to crew.length

then, you should log "Invalid crew indices" to the console and call return to exit the function.

You can use the OR operator || to check multiple conditions at once:

js
if (condition1 || condition2 || condition3 || condition4) {
  console.log("Message");
  return;
}

--hints--

You should validate indices using an if statement.

js
const cleaned = __helpers.removeJSComments(
  swapCrewMembers.toString()
);
assert.match(
  cleaned,
  /\bif\s*\([^)]*\)/,
  "Your function must use at least one if statement to validate indices"
);

You should first validate indices and, if invalid, log the message "Invalid crew indices" before exiting the function using return.

js
const spy = __helpers.spyOn(console, "log");

try {
  const crew = [
    { id: 1 }, 
    { id: 2 }, 
    { id: 3 }, 
    { id: 4 }, 
    { id: 5 }
  ];
  swapCrewMembers(crew, -1, 2);
  swapCrewMembers(crew, 1, 10);
  swapCrewMembers(crew, 15, 3);
  swapCrewMembers(crew, -5, 99);
  swapCrewMembers(crew, 2, -1)
  swapCrewMembers(crew, 2, 3);
  assert.lengthOf(
    spy.calls,
    5,
    "Each invalid call should log exactly once"
  );
  spy.calls.forEach(call => {
    assert.match(
      call[0],
      /^Invalid crew indices$/i,
      "Each log must be 'Invalid crew indices'"
    );
  });
} catch (err) {
  assert.fail(err);
} finally {
  spy.restore();
}

Immediately after logging the message in the validation check, invoke return to exit the function early.

js
const cleaned = __helpers.removeWhiteSpace(__helpers.removeJSComments(swapCrewMembers.toString()));
const logReturnRegex = /console\.log\s*\([^)]*\)\s*;\s*return\b/g;
const matches = cleaned.match(logReturnRegex) || [];
const allLogs = cleaned.match(/console\.log\s*\([^)]*\)/g) || [];
assert.strictEqual(
  matches.length,
  allLogs.length,
  "Every console.log(...) must be immediately followed by return"
);

const spy = __helpers.spyOn(console, "log");
try {
  const source = __helpers.removeJSComments(swapCrewMembers.toString());
  const instrumentedSource = source.replace(/\}\s*$/, 'console.log("TEST"); }');
  const instrumentedFn = eval("(" + instrumentedSource + ")");
  instrumentedFn([{ id: 1 }, { id: 2 }], -1, 1);
  assert.lengthOf(spy.calls, 1);
  assert.match(spy.calls[0][0], /invalid\s+crew\s+indices/i);
} finally {
  spy.restore();
}

When given valid inputs, your swapCrewMembers function should not log any error messages and should not exit early.

js
const spy = __helpers.spyOn(console, "log");
try {
  const crew = [
    { id: 1 }, 
    { id: 2 }, 
    { id: 3 }, 
    { id: 4 }, 
    { id: 5 }
  ];
  swapCrewMembers(crew, 1, 3);
  assert.lengthOf(
    spy.calls,
    0,
    "Valid indices should not trigger any console.log"
  );
} catch (err) {
  assert.fail(err);
} finally {
  spy.restore();
}

--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) {
--fcc-editable-region--

--fcc-editable-region--
}