Basic Algorithm Scripting - Slice and Splice

Tell us what’s happening:
I am tiring the run this challenge but the out I am getting, I am confused what is my output vs what should be

I see following in console

// running tests

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

should return

[4, 1, 2, 3, 5]

.

frankenSplice([1, 2], ["a", "b"], 1)

should return

["a", 1, 2, "b"]

.

frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2)

should return

["head", "shoulders", "claw", "tentacle", "knees", "toes"]

. All elements from the first array should be added to the second array in their original order.

frankenSplice([1, 2, 3, 4], [], 0)

should return

[1, 2, 3, 4]

. // tests completed // console output [ 4, 5, 1, 2, 3 ] [ 4, 5, 1, 2, 3, 6 ] [ 1, 2, ‘a’, ‘b’ ] [ ‘claw’, ‘tentacle’, ‘head’, ‘shoulders’, ‘knees’, ‘toes’ ] [ 1, 2, ‘a’, ‘b’ ] [ 1, 2, ‘a’, ‘b’ ]

Your code so far

function frankenSplice(arr1, arr2, n) {
  //start by printing arr2, 
  // insert array1 in the location of n of second array
  let combined = [];
  for(let x = 0; x < arr2.length;x++){
    combined.push(arr2[x])
    if(x === n){
      //if index of x === n
      combined.splice(arr2[x],0,arr1)
      //combined.push(arr1)
    }
  }
  let result = combined.flat()
  console.log(result)
  return result;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Slice and Splice

Link to the challenge:

You’re not too far from a solution here but there are a couple of issues getting in the way.

Firstly, the first parameter of your splice isn’t correct. It should be the index at which you start inserting/deleting values.

Secondly, using a for loop here is problematic because one of the tests requires you to account for the second array being empty. This would mean that your for loop doesn’t do anything because arr2.length is 0.

However, you don’t need a loop.
You can first clone arr2 into combined in a single command (without direct assignment as that would then cause arr2 to be mutated when you modify combined).
Then, you can simply splice in arr1 at the required index.

1 Like

Thanks for explaining, now my code is as follows but it is not working

function frankenSplice(arr1, arr2, n) {
  //start by printing arr2, 
  // insert array1 in the location of n of second array
  let combined = [...arr2, arr2.splice(arr2[n],0,arr1)]
  console.log(combined)
  return combined;
}

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

The output in console is (4) [4, 5, 6, Array(0)] . I tried it many ways, but I do not understand why my last index is a Array with 0 element.

Edit: Even when I […] spread arr2, and use splice on it, it doesn’t have a affect. e.g.

let combined = [...arr2, ...arr1.splice(1,0,"hello")]
  console.log(combined);
let combined = [...arr2, arr2.splice(arr2[n],0,arr1)]

You might wrongly insert the first argument of .splice( )
About splice(), you can read more example here. JavaScript Array splice() Method

As I said, the first parameter in the splice method should be the index at which you start the splice. You’re trying to use an array value rather than an index number.

Even though I am using let combined = [...arr2, arr2.splice(n,0,arr1)]; i expect to see something else but when I see in dev tools console I see (4) [4, 5, 6, Array(0)].

What I expect to see is a array instead of Array[0]

From my example [...arr2, arr2.splice(n,0,arr1)] I expect splice arguments be like insert arr1 at position n, where n is the number of index number. Is my thinking right?

arr2.splice(n,0,arr1)

Be careful on splice( ), your usage says:

  1. At index n, to remove 0 number of item from arr2.
  2. then, at that n location, add array of arr1 into arr2.
  3. finally, it returns array of item that was REMOVED.
frankenSplice([1, 2, 3], [4, 5, 6], 1);
let combined = [...arr2, arr2.splice(n,0, ...arr1)]
// then combined will become this:
combined = [ [4,5,6] , nothing ]

if you wish to overwrite your original arr2 with arr1 item added, why don’t you split it to 2 statement ? unless you want to get those “removed item” as return.

I m confused, so according to splice docs array.splice(index, howmany, item1, ....., itemX) here n is index, howmany to remove which is 0 or none, and item1 (items to add) is arr1, Do I understand it right?

Note:

The input arrays should remain the same after the function runs.

But:

The splice() method changes the contents of an array

You probably don’t want to splice

Yes. if you with to add in arrrayitem of arr1. you can use spread operator “…”
Then i would check RETURN output of these:

 frankenSplice([1, 2, 3], [4, 5, 6], 1);
 console.log(arr2)                          // [ 4, 5, 6 ]
 console.log(arr2.splice(1,1,999))          // [ 5 ]
 console.log(arr2)                          // [ 4, 999, 6 ]
 console.log(arr2.slice())                  // [ 4, 999, 6 ]
 console.log(arr2)                          // [ 4, 999, 6 ]

If i can see how those 5 are being console out, i can choose the combination method to solve the challenge.

I got ADHD so when I see ‘slice and splice’ I am fixated on how it should be used.

Yeah, you see the word ‘splice’ and you focus only on splice. The idea here is that you ‘frankensplice’ - do something like a splice without actually changing the input arrays. But splice changes the input arrays, so you can’t use splice on the input arrays.

1 Like

Following is my code now, I do not know where and why I m going wrong, a hint would help

function frankenSplice(arr1, arr2, n) {
  let second = [...arr2]
  let newOne = []
  //console.log(cloned);
  for(let i = 0; i < second.length; i++){
    newOne.push(second[i])
    if(i === n){
      //console.log(second[i])
      newOne.push(arr1)
    }
  }
  let flat = newOne.flat();
  console.log(flat)
  return newOne;
}

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

As my code reads,
On first iteration i = 0 which is less then second length which is 3, so it pushes the value of index 0 to newOne, after which it check if index === n number which is false, so second iteration.

On second iteration i = 1 which is less then second length which is 3, so it pushes the value of index 1 to newOne, after which it check if index === n number which is so it pushes arr1 into the newOne,

On third iteration i = 2 which is less then second length which is 3, so it pushes the value of index 1 to newOne, after which it check if index === n number which is false, so fourth iteration.

On fourth iteration i = 3 which is = to second length which is 3,so it exist the loop

Is my logic right? and where am I missing or going wrong?

Anyone to lend a hand and help me figure it out?

You’ve worked out how to do the first part. So, with a single command, just do the second part. No need for any loops or anything more complicated than a single line of code.
Then you just return your new array (having flattened it).
It’s not the only solution but it’s one of the simplest and, despite using the splice() method, it doesn’t modify either of the input arrays.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.