[SOLVED] Basic-algorithm-scripting/slice-and-splice

Tell us what’s happening:

Can anyone help me return the value of someSplice as stated in my code. I don’t have any idea why it doesn’t return the value.

Your code so far


function frankenSplice(arr1, arr2, n) {
// It’s alive. It’s alive!

 let someSlice = arr1.slice(0, arr1.length);    
 let someSplice = arr2.splice(n, 0, ...someSlice);

return someSplice;
}

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

The only problem I had is The second array should remain the same after the function runs. as stated by FCC challenge.

This code works but I need a way to not change arr2.

function frankenSplice(arr1, arr2, n) {
// It’s alive. It’s alive!

let someSlice = arr1.slice(0, arr1.length);    
 arr2.splice(n, 0, ...someSlice);

return arr2;
}

 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/66.0.3359.181 Safari/537.36.

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

1 Like

From mdn https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

The splice() method changes the contents of an array by removing existing elements and/or adding new elements.

Take a look on mdn for the return value of slice() and you should be good to go.

2 Likes

@JM-Mendez, Got it, Thanks for the link.

I’ve been thinking how to complete this challenge without changing arr2 But to no avail . Do you have any idea how to complete this challenge?

1 Like

The definition of slice() on mdn doesn’t say this specifically, but if you use array.slice() with no arguments or array.slice(0), it creates a copy of the array.

1 Like

Did you figure it out?

I ended up copying the second array instead of the first one, because technically you don’t even need to copy the first array

So

let arrCopy = arr2.slice(0, arr2.length);
let newArr = arrCopy.splice(n, 0, ...arr1);

return newArr;
3 Likes

I feel like this exercise is so misleading because the starter code in the editor has “return arr2” at the end.

1 Like

function frankenSplice(arr1, arr2, n) {

arr2.splice(n, 0, …arr1);

return arr2;
}

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

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

Getting all the challenges except the last one.
Could someone help explain how to keep the second array the same while the function runs?

I just finished the challenge…

function frankenSplice(arr1, arr2, n) {

let NewArr = arr2.slice(0,arr2.length)
NewArr.splice(n, 0, …arr1);

return NewArr;
}

this is the function i used… somehow it works…

6 Likes

What I did was probably cheating–because I never actually use slice()–but I used Array.from() to make a deep copy of arr2.

Hey guy, here a solution which is more readable. Using spread operation :slight_smile: :

function frankenSplice(arr1, arr2, n) {
  let arr2Out = […arr2];
  console.log(n +"~ “+ arr2 +”<< "+ arr1);
  arr2Out.splice(n, 0, …arr1);
  console.log(arr2Out);
  return arr2Out;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);
2 Likes

This is perfect, I also solved the same way.

If you’re simply copying the entire array, then there’s no need to set parameters for slice. The code below will copy arr2 into NewArr.

let NewArr = arr2.slice();

I’m not sure why but this isn’t working. I had to write it like this:

  let arrCopy = arr2.slice(0, arr2.length);
  arrCopy.splice(n, 0, ...arr1);
  return arrCopy;
1 Like

Using .slice():

  let arr3 = arr2.slice();
  arr3.splice(n, 0, ...arr1);
  return arr3;

Using .map():

  let arr3 = arr2.map(x=>x);
  for (let i = arr1.length-1; i >= 0; i--){
    arr3.splice(n, 0, arr1[i]);
  }
  return arr3; 
1 Like

Parameters of slice() is optional, its default is to slice out entire array, so:

const frankenSplice = (arr1, arr2, n) => {
  let arr = arr2.slice()
  arr.splice(n,0,...arr1)
  return arr
}
4 Likes

The trick with this one is using the rest operator “…” inside of the splice parameters. Otherwise, you insert an array rather than it’s components.

1 Like

It wasn’t working because the splice() method is used to remove/add elements from/to array, and it returns ‘an array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned’ - MDN

tryingmybest was returning an array of deleted elements, which was empty.

3 Likes

function frankenSplice(arr1, arr2, n) {
// It’s alive. It’s alive!
let someSplice = arr2.slice(0, arr2.length);
someSplice.splice(n, 0, …arr1);

return someSplice;
}

I believe this challenge doesn’t allow u to use .length .
So use the spread insteand.