Slice and Splice FreeCodeCamp

Slice and Splice FreeCodeCamp
0

#1

Tell us what’s happening:
I achieved to return all numbers but they are returning as string, how could I return them as an array?

Your code so far

function frankenSplice(arr1, arr2, n) {
var newArray = new Array();
newArray = arr2.slice(0,n);
newArray += newArray.splice(n,0,arr1,arr2.slice(n,arr2.length));
return newArray;
}

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


#2

Hi Leo,

Please share the challenge link.

I also found one bug with your code. It’s not a bug actually, but please note.

First code means newArray is a new empty array.
But in second line, you ignore the value(above) and assign another value to newArray var.

Also please note splice applies the changes on object, and I 'm not sure if it return anything.


#3

Sorry, that’s the link.
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice/


#4

Ok, beside even you log the result(newArray) and you think it’s correct but it’s not.
this line

Seems create a String, rather than an array.

You need to add elements of first array to n position of second array, please considering following notes:

typeof(<<any_var>>) tells you the type of the given input. If you console.log(newArray) before you return it, you see the type is string, while test expected an array.

You may use a for loop to read each element of first array and add it to second array.

Also note splice an array into another array like arr1.splice(n,0,arr2) will put the whole arr2 as one item to arr1

TIP: using ...arr1 will refer to items of arr1

Feel free to ask if you still think more hints.

Keep going on great work, happy coding.


#5

I’ll need more hints, please.

I tried to use .push() to insert , but it didn’t work.

I think I’m not including into my new array at the right way.

I’m not that far from the answer but I’m not reaching it.


#6

This might help. The MDN is your friend.


#7

function frankenSplice(arr1, arr2, n) {
var newArray = [];
newArray.splice(0,0,arr2.slice(0,n));
newArray.splice(n,0,arr1,arr2.slice(n,arr2.length));
return newArray;
}

I almost got it now.

But it made three different arrays, and I want only one.


#8

There was another suggested array method suggested too. Very handy for copying an array.


#9

Push add the element at the end index, it doesn’t work here, unless you like this:

  1. push the first elements of arr2 before n with for loop, or slice it as you did.
  2. Then in a for loop, push items of arr1 to result(newArray).
  3. At the end, have another loop to push elements left from arr2 to the result.

is wrong.
I note my tip again. Please considering following example:

var n=1;
var a0=[1,2,3,4];
var a1=[5,6,7];
a0.splice(n,0,a1);
console.log(a0);//logs  Array [1, Array [5, 6, 7], 2, 3, 4]

As you see in above sample(the same way you do in your code), splice add while array a1 as one element, rather it’s elements.

Here you go comrade,

More hint:
Have a for loop, to iterate over all elements of a1 array, add access each element of a0 items, and add them using splice to a0 array

var n=1;
var a0=[1,2,3,4];
var a1=[5,6,7];
for(var b=0;b<a1.length;b++){
var item=a1[b];
a0.splice(n,0,item);//it has one small bug! try to figure out
//console.log(a0);
}
console.log(a0);//logs Array [1, 7, 6, 5, 2, 3, 4] , note the order is wrong, find the bug to fix

Feel free to ask for more hint comrade, but try to fix it, it’s so easy, and you are almost done.

Happy programming.


#10

This is more or less what you are doing now

// make a new empty array as a copy

// insert arr2 into that copy using suggested methods

// insert arr1 into your copy at the proper place using suggested methods

// return your copy 

You are inserting an entire array as a single element [1,2,3] to your new array. How can you insert these as individual elements 1,2,3?

// make a new empty array as a copy

// insert each individual element of arr2 into that copy using suggested methods

// insert  each individual element of arr1 into your copy at the proper place using suggested methods

// return your copy 

You could also simplify this

//  make a copy of arr2 using suggested methods

// insert  each individual element of arr1 into your copy at the proper place using suggested methods

// return your copy 

#11

OMG, I can’t believe I got it, I know my code can be a lot better, bit it passed all the tests.

I understood all your hints, it took me like half an hour and now I see where was my mistake.

function frankenSplice(arr1, arr2, n) {
  var newArray = [];
  for (var i=0; i<arr2.length/2;i++){
    newArray.splice(0+i,0,arr2.slice(0,n)[i]);
  }
  for (i=0; i<arr1.length;i++){
    var item = arr1[i];
    newArray.splice(n+i,0,item);
  }
  for (var i=0; i<arr2.length/2;i++){
    newArray.splice(newArray.length+i, 0, arr2.slice(n,arr2.length)[i]);
  }
  return newArray;
}

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

That’s the code it passed. Thank you so much for helping me.

I’ll help others when I get better. See ya.


Can I use slice and splice on the same line?
#12

Awesome! You made it.

please note you may turn the first loop into one splice call, no need to have a loop and insert elements one by one using splice.

Second for loop looks promising, great. You got the point about the n+1, very good.

You can use push for last loop too.

Now let’s make it better.

Hit: The second for loops is correct, the splice you call (newArray.splice(n+i,0,item);) adds the item to the given index(n+1). No matter if there is anything after n+1 or before it.
Another Hit: using slice to copy an array, just like what you did before.
Another hit: you can ignore the first loop and last loop by one slice call before 2nd loop.

I believe you come up with optimized code pal, keep it up.

Happy programming.


#13

function frankenSplice(arr1, arr2, n) {
let arr3=arr2.slice(0);
for(let i=0;i<arr1.length;i++){
arr3.splice(n+i,0,arr1.slice(0)[i]);
}
return arr3;
}

I think it is more easier to do like above.


#14

Good!

One note:

the line arr3.splice(n+i,0,arr1.slice(0)[i]); could be: arr3.splice(n+i,0,arr1[i]);