curriculum/challenges/english/blocks/workshop-space-mission-roster/694f7cae8b5aa1f91cb33c8b.md
As before, you should validate your input. Specifically, if size is less than 1, you should log "Chunk size must be >= 1" to the console and then call return to exit the function.
If the input size is invalid, you should log the following error message to the console, "Chunk size must be >= 1".
const spy = __helpers.spyOn(console, "log");
try {
const result1 = chunkCrew([], 0);
const result2 = chunkCrew([], -5);
const result3 = chunkCrew([1, 2, 3], 2);
assert.isUndefined(result1);
assert.isUndefined(result2);
assert.lengthOf(spy.calls, 2);
assert.match(spy.calls[0][0], /chunk\s*size\s*must\s*be\s*>=\s*1/i);
assert.match(spy.calls[1][0], /chunk\s*size\s*must\s*be\s*>=\s*1/i);
} catch (err) {
assert.fail(err);
} finally {
spy.restore();
}
After logging the error message, you should immediately call return to exit the function early.
const cleaned = __helpers.removeWhiteSpace(
__helpers.removeJSComments(chunkCrew.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(chunkCrew.toString());
const instrumentedSource = source.replace(
/\}\s*$/,
'console.log("SENTINEL"); }'
);
const instrumentedFn = eval("(" + instrumentedSource + ")");
instrumentedFn([], 0);
assert.lengthOf(
spy.calls,
1,
"Function must return immediately after logging validation error"
);
assert.match(
spy.calls[0][0],
/chunk\s*size\s*must\s*be\s*>=\s*1/i
);
} finally {
spy.restore();
}
Your chunkCrew function should pass validation without errors when size is greater than or equal to 1.
const spy = __helpers.spyOn(console, "log");
try {
chunkCrew([], 1);
chunkCrew([1, 2, 3], 2);
assert.lengthOf(
spy.calls,
0,
"Valid size should not trigger console.log"
);
} 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) {
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;
}
const EVAReadySquad = getEVAReadyCrew(updatedSquad);
function chunkCrew(crew, size) {
--fcc-editable-region--
--fcc-editable-region--
}