Basic Algorithm Scripting: Solution not being accepted

Basic Algorithm Scripting: Solution not being accepted
0

#1

I’m at the Slice and Splice challenge. The problem is:

You are given two arrays and an index.
Use the array methods slice and splice to copy each element of the first array into the second array, in order.
Begin inserting elements at index n of the second array.
Return the resulting array. The input arrays should remain the same after the function runs.

My code for this is as follows:

  console.log(`given arrays are: \n arr1:${arr1} \n arr2:${arr2}\n and the index at which to insert is ${n} `);
  var result = arr2.slice(0, n);
  console.log(`Step 1: result array is now ${result} `);
  result.push(...arr1);
  console.log(`Step 2: result array is now ${result} `);
  result.push(arr2.slice(n));
  console.log(`Step 3: result array is now ${result} `);
  return result;
}

I’ve added a few console lines to see what the intermediate steps are producing. And the result being returned is the correct one. Yet this challenge does not complete. What is going wrong?


#2

Your solution is not returning the correct results.


#3

I don’t get it
What’s wrong with the result?


#4

You can run the code in the repl.it that I included. See how for that first case you are returning [4, 1, 2, 3, [5]]? That is not the same thing as [4, 1, 2, 3, 5]. If you uncomment the other tests, you’ll see similar problems.


#5

Thank you! I see my mistake now, and I solved it. Adding the spread operator before the arr2.slice() unpacks the array resulting from the slice and returns the array’s elements to push().


#6

Good job!