Slice and Splice Alternatives

Slice and Splice Alternatives
0

#1

Tell us what’s happening:
I’m having some trouble with this. I have a couple of ways I wanted to try it down below.

What’s next?

Your code so far


function frankenSplice(arr1, arr2, n) {
let arrLength= arr1.join(''); //gives a value to associate with length in for loop
let tempArr=[]; //array to hold results of for loop
for (var i=0; i<arrLength.length;i++){
let slice = arr1.slice(0, [i]); //loops and holds content of arr1 nomatter the size
tempArr.push(slice); //pushes held content to tempArr
}
let newArr = arr2.splice(n, 0, tempArr); //splices in tempArr contents to arr2 into newArr
  return newArr;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);

The second route is as follows:

function frankenSplice(arr1, arr2, n) {
let newArr = arr2.splice(n, 0, [...arr1]);
return newArr;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) 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

First, a comment about code readability. To make it easier for others to read your code, you should properly indent your code blocks. For example, your first function should look something like this:

function frankenSplice(arr1, arr2, n) {
  let arrLength= arr1.join(''); //gives a value to associate with length in for loop
  let tempArr=[]; //array to hold results of for loop
  for (var i=0; i<arrLength.length;i++){
    let slice = arr1.slice(0, [i]); //loops and holds content of arr1 nomatter the size
    tempArr.push(slice); //pushes held content to tempArr
  }
  let newArr = arr2.splice(n, 0, tempArr); //splices in tempArr contents to arr2 into newArr
  return newArr;
}

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

Now about the code you have written. The line below is not doing what you think it should be doing.

let slice = arr1.slice(0, [i]); 

The argument values passed to the slice method should be integers. The first argument is the starting index and the second argument is the index which slice will extra up to but not include.

You are using an array for the second argument which does not work. Just use a number value or a variable containing a number value for this end index. Also, you have specified 0 as the starting index for the slice. This will always start the slice at the beginning of arr1. Is that what you want to do?

The instructions tell you that the input arrays should remain the same after the function runs. This means do not mutate the original arrays. In the following line, you are mutating arr2, because splice changes the array in-place.

let newArr = arr2.splice(n, 0, tempArr);


In the second function, you are still mutating arr2 and not only that the return value of the splice method is an array of any items removed during the splice. Since you are specifying 0 for the 2nd splice argument, your splice returns an empty array and assigns it to newArr.

There are some other issues with this solution, but see if you can fix the ones I have identified first.