Back to Freecodecamp

Step 24

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

latest4.6 KB
Original Source

--description--

To create chunks of size n from an array without changing it, you can combine a for loop with the slice() method as such:

js
const result = [];
for (let i = 0; i < array.length; i += n) {
  result.push(array.slice(i, i + n));
}

The example above:

  • creates an empty array named result

  • loops through the original array in steps of size n

  • creates chunks from array of size n and pushes them into result using slice()

To complete the chunkCrew function, you must:

  • create an array named chunks

  • loop through crew in steps of size size, creating and pushing chunks from crew into chunks using slice()

  • return the chunks array

--hints--

You should create an empty array named chunks.

js
const funcStr = __helpers.removeJSComments(chunkCrew.toString());
assert.match(
  funcStr,
  /(var|let|const)\s+chunks/,
  "You must have at least one space between the declaration keyword (let/const) and 'chunks'"
);
const cleaned = __helpers.removeWhiteSpace(__helpers.removeJSComments(chunkCrew.toString()));
assert.match(cleaned, /(?:^|[^a-zA-Z])(var|let|const)chunks=\[\]/);

You should use a for loop to build chunks.

js
const cleaned = __helpers.removeWhiteSpace(__helpers.removeJSComments(chunkCrew.toString()));
assert.match(cleaned, /for\s*\(/);

Inside the for loop, you should call push() on the chunks array to insert every sliced chunk (which must be created by calling slice() on the crew array).

js
const cleaned = __helpers.removeWhiteSpace(__helpers.removeJSComments(chunkCrew.toString()));
assert.match(cleaned, /chunks\.push\(crew\.slice\(/);

After the loop, you should return the chunks array.

js
const cleaned = __helpers.removeWhiteSpace(__helpers.removeJSComments(chunkCrew.toString()));
assert.match(cleaned, /returnchunks;?}$/);

Your chunkCrew function should return chunks of length size from the input array crew without mutating it.

js
const sample = [1, 2, 3, 4, 5];
const copyBefore = [...sample];
const chunks = chunkCrew(sample, 2);
assert.isArray(chunks);
assert.deepEqual(chunks, [[1, 2], [3, 4], [5]]);
assert.deepEqual(sample, copyBefore, 'chunkCrew should not mutate the original crew');

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

--fcc-editable-region--
}