Build a Space Mission Roster - Step 12

Tell us what’s happening:

What is the point of the UNUSED copy of the copy (updatedCopy)?
The test does not pass without this redundant data. Yet, in the next lesson, it is not there. A lot of frustration was caused by that.

Your code so far

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();
  const updatedCopy = updatedCrew.splice(); // This is pointless. Why force this to pass the test?
  
// User Editable Region
  updatedCrew[fromIndex] = updatedCrew.splice(toIndex, 1, updatedCrew[fromIndex])[0];

}
// User Editable Region

Your browser information:

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

Challenge Information:

Build a Space Mission Roster - Step 12

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/workshop-space-mission-roster/694d8ddd106340237efb0c80.md at main · freeCodeCamp/freeCodeCamp · GitHub

The purpose of using a copy array is explained, so as not to mutate the original array. In the seed code you are already given an updated crew copy to work on so you don’t need to create another copy. The updated crew copy remains there in the next step.

Step 12 passed for me just using the seed code and the line you added in the editable region.

Thank you for your reply.
In the previous lesson I created the shallow copy (updatedCrew), that already prevents mutation of the original array. In this lesson, a copy of that copy was required but never used. That is what makes it pointless. In the next lesson, that copy is not in the code.

The task would not pass without that additional copy.

I just realised, that the updated copy is a splice() not slice(). Nevertheless, it is unused. It’s odd.

The instructions don’t ask you to add this. Are you saying it passed for you with this line? As you say you used splice instead of slice for the copy. The instructions showed you how to do the whole thing including making a shallow copy but expects you to look and see you don’t need to make another shallow copy.


  const updatedCopy = updatedCrew.splice(); // This is pointless. Why force this to pass the test?

Precisely. The code was as is, minus that line, and it always failed. I saw the same instruction to copy the updatedCrew from what I remember.

I had the line: `updatedCrew[fromIndex] = updatedCrew.splice(toIndex, 1, updatedCrew[fromIndex])[0];`

Socrates:
Your code calls directly on , which mutates the original array instead of using a copy. Create a copy of with and apply the one‑line technique to that copy, then log the result and try again.

OK, it also passed for me with the shallow copy added with splice in error. It also passes if you put ‘slice()’ instead of splice on the extra copy and as I said it worked for me without the additional copy. I’ll log an issue about this as it shouldn’t be passing errors or code that isn’t needed.

Perhaps there was a browser glitch. I went back to that task, and it worked without the additional copy, and only the one-liner.

Thank you for your responses.