curriculum/challenges/english/blocks/workshop-space-mission-roster/69504c249850e861921dca82.md
You may have noticed that EVAChunks is essentially an array with arrays inside of it, or a two-dimensional (2D) array. A nested for loop can be used to log data from a 2D array:
for (let i = 0; i < rootArray.length; i++) {
console.log(`Group ${i + 1}:`);
for (let j = 0; j < rootArray[i].length; j++) {
console.log(rootArray[i][j].property);
}
}
In the example above, rootArray is the main array, rootArray[i] is a sub-array inside of the main array, and rootArray[i][j] is one object inside of that sub-array and .property accesses a value in that object.
Use a nested for loop to iterate through EVAChunks and log the name of every astronaut in each chunk. Be sure to:
EVAChunks as your root arrayChunk ${i+1}: between chunksYou should use a nested for loop to iterate through EVAChunks and log the name of every astronaut in each chunk. Log Chunk ${i+1}: between chunks.
const cleaned = __helpers.removeWhiteSpace(__helpers.removeJSComments(code));
assert.match(
cleaned,
/for\([^)]*EVAChunks[^)]*\)\{[^}]*console\.log[^;]*Chunk\$\{[^}]+\}:/,
'First for loop should iterate EVAChunks and log Chunk ${i+1}:'
);
assert.match(
cleaned,
/for\([^)]*EVAChunks[^)]*\)\{[\s\S]*?for\([^)]*EVAChunks[^)]*\)[\s\S]*?console\.log[^;]*EVAChunks\[[^\]]+\]\[[^\]]+\]\.name/,
'Nested for loop should iterate inner EVAChunks elements using double indexing and log .name'
);
function captureConsoleFromCode(code) {
const logs = [];
const originalLog = console.log;
console.log = (...args) => logs.push(args.join(" "));
try {
const userScriptFn = new Function(code);
userScriptFn();
} finally {
console.log = originalLog;
}
return logs;
}
const logs = captureConsoleFromCode(code)
const expectedLogs = [
"Chunk 1:", "Elise", "Felix", "Irene",
"Chunk 2:", "Caroline", "Andy", "Hank"
];
assert.strictEqual(
logs.length,
expectedLogs.length,
"You must log each chunk and each astronaut name exactly once"
);
assert.deepEqual(
logs,
expectedLogs,
"Console output must log 'Chunk #' followed by the names of astronauts in each chunk in order"
);
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) {
if (size < 1) {
console.log("Chunk size must be >= 1");
return;
}
const chunks = [];
for (let i = 0; i < crew.length; i += size) {
chunks.push(crew.slice(i, i + size));
}
return chunks;
}
const EVAChunks = chunkCrew(EVAReadySquad, 3);
--fcc-editable-region--
--fcc-editable-region--