Slice and Splice - I think this is right?

Slice and Splice - I think this is right?
0

#1

Tell us what’s happening:
I’m fairly certain the code I have is valid.

Made a copy of arr2 so we don’t directly modify it, then using splice to insert at n, then return arrCopy

Your code so far


function frankenSplice(arr1 = [], arr2 = [], n = 0) {
  // It's alive. It's alive!
  const arrCopy = [...arr2]
  arrCopy.splice(n, 0, arr1);
  return arrCopy;
}

const results = [
  frankenSplice([1, 2, 3], [4, 5], 1),
  frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2),
  frankenSplice([1, 2], ["a", "b"], 1)
];
results.forEach(result => console.log(result)); 

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice/


#2

For test1,

Notice you are also creating another array and returning

=> [ 4, [ 1, 2, 3 ], 5 ]

instead of

[4, 1, 2, 3, 5].


#3

Expanding on what @shimphillip showed you, after the first and second arguments are supplied to the splice method, you can pass in extra argument values which you want to insert into the array at the index specified by the first argument. An array counts as a single argument. Think how you could “spread” arr1 so it’s elements are the extra arguments to the splice method.


#4

This is really helpful! I should honestly switch to an editor when debugging this because this console.log doesn’t show me what you put.


#5

Are you using your browser’s console? If so, then you would definitely see the result @shimphillip is referring to.


#6

Nah I was console.log’ing with FreeCodeCamps, it didn’t show the results inside brackets like how @shimphillip had suggested


#7

The only true way of seeing the results of console.log is to use your browser’s console. In Chrome, you use Ctrl+Shft+J to access the console.