Build a Space Mission Roster - Step 30

Tell us what’s happening:

It says “You should invoke sortByPriorityDescending() with the sorted array.” meanwhile the sorted array has been invoked.

``function printCrewSummary(crew) {
const sorted = crew.slice();

let tasks = [
{ id: 1, priority: 2 },
{ id: 2, priority: 5 },
{ id: 3, priority: 1 }
];

// sortByPriorityDescending()
function sortByPriorityDescending(arr) {
// Use slice() to avoid mutating the original array (best practice)
return arr.slice().sort((a, b) => b.priority - a.priority);
}
``

Your code so far

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);


// User Editable Region

function printCrewSummary(crew) {
  const sorted = crew.slice();
  
  let tasks = [
  { id: 1, priority: 2 },
  { id: 2, priority: 5 },
  { id: 3, priority: 1 }
];

// sortByPriorityDescending()
function sortByPriorityDescending(arr) {
  // Use slice() to avoid mutating the original array (best practice)
  return arr.slice().sort((a, b) => b.priority - a.priority);
}

const sortedTasks = sortByPriorityDescending(tasks);


// User Editable Region

}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36

Challenge Information:

Build a Space Mission Roster - Step 30

I suggest you reset the step. You are working on line 99 inside your printCrewSummary function.

The sortByPriorityDescending function has been created earlier in the code. You are asked to call that function on the variable ‘sorted’ just created.

1 Like

Yes, I later figured it out, Thanks a ton :waving_hand:

2 Likes