Build a Space Mission Roster - Step 12

Tell us what’s happening:

bug with Step 12 splice() swap, test fails despite correct one-line implementation

Your code so far

function swapCrewMembers(crew, fromIndex, toIndex) {
  if (
    fromIndex < 0 || 
    toIndex < 0 ||
    fromIndex >= crew.length ||
    toIndex >= crew.length
  ) {

// User Editable Region

    console.log("Invalid crew indices");
    return;
  }

  const updatedCrew = crew.slice();
  updatedCrew[fromIndex] = updatedCrew.splice(fromIndex, 1, updatedCrew[toIndex])[0];

  return updatedCrew;
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0

Challenge Information:

Build a Space Mission Roster - Step 12

The problem is that splice() returns an array of removed elements, not the value you want. When you do:

updatedCrew[fromIndex] = updatedCrew.splice(fromIndex, 1, updatedCrew[toIndex])[0];

You’re trying to assign the first element of the array returned by splice() to updatedCrew[fromIndex], which isn’t the correct approach for swapping.

Instead of using splice(), we can simply swap the values directly:

removed by moderator

I hope it will help you.
If you are interested in DM, contact on telegram(@heislyp).

but…
// running tests

  1. You should perform the swap by calling the splice() method.
  2. You should call splice() with the updatedCrew array to perform the swap. You should use the one-line technique.
    // tests completed
    :distorted_face:

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Guidelines - The freeCodeCamp Forum

Hi @m4x1m ,

const originalArray = [12, 97, 68, 55];
const copyArray = originalArray.slice();
copyArray[1] = copyArray.splice(3, 1, copyArray[1])[0];
console.log(copyArray); // [12, 55, 68, 97]

I suggest reviewing the example again until you fully understand it.

In the example, the fromIndex is 1 and the toIndex is 3.

How does your solution differ?

Happy coding!

Hello! you mean - updatedCrew[fromIndex] = updatedCrew.splice(toIndex, 1, updatedCrew[fromIndex])[0];? if i use this the error persists

You may have inadvertently changed the starting code, which will cause the tests to fail. Please reset this step and try your new code again. If you still have an error, please post your updated code.

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

// running tests
2. You should call splice() with the updatedCrew array to perform the swap. You should use the one-line technique.
// tests completed

:distorted_face:
:distorted_face:
:distorted_face:

It doesn’t look like you reset this step as asked since this was not part of the starting code.

1 Like