curriculum/challenges/english/blocks/workshop-space-mission-roster/694c9c186c089f7bdff0f3c6.md
Generally, it is good practice to validate your input(s) inside a function. Use a for loop to iterate through the crew array and check whether any member already has the same id as the input astronaut. If a duplicate is found, log the error message console.log("Duplicate ID: " + astronaut.id) and then call return to exit the function early.
Here is an example of this commonly used technique:
if (existingMember.id === newMember.id) {
console.log("Duplicate ID: " + newMember.id);
return;
}
Your function should use a for loop to scan existing crew members (any valid for (...) form is fine).
assert.match(__helpers.removeJSComments(code), /for\s*\([^)]*\)/);
Inside the for loop, you should check for a duplicate id. If one is found, log "Duplicate ID: " + astronaut.id" to the console and then call return to exit the function.
const spy = __helpers.spyOn(console, "log");
try {
const crewWithDuplicate = [
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 4 },
{ id: 3 }
];
addCrewMember(crewWithDuplicate, { id: 3 });
assert.lengthOf(
spy.calls,
1,
"Duplicate ID input should trigger exactly one console.log and then exit"
);
assert.match(
spy.calls[0][0],
/duplicate\s+id\s*:\s*3/i,
"Console message should mention 'duplicate id'"
);
} catch (err) {
assert.fail(err);
} finally {
spy.restore();
}
When given valid inputs, your addCrewMember function should not invoke a console.log() error message or an early return exit.
const spy = __helpers.spyOn(console, "log");
try {
const crewNoDuplicate = [
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 4 }
];
addCrewMember(crewNoDuplicate, { id: 5 });
assert.lengthOf(
spy.calls,
0,
"A truly new astronaut should produce zero console.log calls"
);
} catch (err) {
assert.fail(err);
} finally {
spy.restore();
}
const squad = [];
const firstAstronaut = {
id: 1,
name: "Andy",
role: "Commander",
isEVAEligible: true,
priority: 3
};
function addCrewMember(crew, astronaut) {
--fcc-editable-region--
--fcc-editable-region--
}