Slice and Splice is kicking my butt

Slice and Splice is kicking my butt
0

#1

Tell us what’s happening:
I tested my code in the console I am just getting an empty array. I am not sure what I am doing wrong

Your code so far


function frankenSplice(arr1, arr2, n) {
   let franken = arr2.slice(0); 
   let stien = franken.splice(n, 0, ...arr1);
   return stien;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);

Your browser information:

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

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


#2

Splice returns an array of the items you remove (the 2nd parameter). Since you are not removing any items (the 0), you get an empty array. You should not be assigning the splice to another variable. You just need to execute the splice on franken and then return franken.

The lesson to learn here is slice creates a new array and splice mutates an array and returns an array containing any items removed (specified by the 2nd parameter).


#3

Thank you so much that helped a lot.


#4
Slice and Splice  *****my solution******

function frankenSplice(arr1, arr2, n) {

var array = []; var array1 = [];

for(var i = 0; i < arr2.length; i++){

array.push(arr2[i]);}

for(var k = 0; k < arr1.length; k++){

array1.push(arr1[k]);}

array1.reverse();

for(var j = 0; j < array1.length; j++){

array.splice(n,0,array1[j]);}

return array;

}