Basic Algorithm Scripting - Slice and Splice

Tell us what’s happening:
The code is working fine but my else condition isn’t working. following is my else if condition

else if(arr2.length == 0){ //here I am checking if the length is 0 that means nothing is in the array then push each item of arr1 into the combo
      arr1.map((item)=>{
        combo.push(item)
      })
    }

What am I doing wrong or missing?
Your code so far

function frankenSplice(arr1, arr2, n) {
  //combine the both array
  let combo = [];
  arr2.map((item, index)=>{
    if(index == n){
      arr1.map((item)=>{
        combo.push(item)
      })
    }else if(arr2.length == 0){
      arr1.map((item)=>{
        combo.push(item)
      })
    }
    combo.push(item)
  })
  console.log(combo)
  return combo
}

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/108.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Slice and Splice

Link to the challenge:

I tried checking this

else{
      console.log("length ", arr1)
    }

it gives me [1,2,3]
but it was four elements. I m very confused.

That’s probably because you did not change the function call at the bottom of your code. Try adding a console.log just above your if condition with the string “test” in it, also maybe item and index as well. Then change that function call from one that is passing to the one that is failing and see if you notice anything.

@mahassan I ran your code solution on the challenge, and it’s only failing with this test:

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].

Your code was traversing the arr2 array first, it’s okay if the array is not empty, but the problem is what if the arr2 is empty? You currently do not have a condition to catch this case.

I do when it says else if condition. ?

I am more confused with the following output

test 4 0
test 5 1
[ 4, 1, 2, 3, 5 ]
[]
test a 0
test b 1
[ 'a', 1, 2, 'b' ]
test head 0
test shoulders 1
test knees 2
test toes 3
[ 'head', 'shoulders', 'claw', 'tentacle', 'knees', 'toes' ]
[]
test a 0
test b 1
[ 'a', 1, 2, 'b' ]
test a 0
test b 1
[ 'a', 1, 2, 'b' ]

It won’t work because your condition to check if arr2 is empty was nested in the arr2.map(() => {...}).

Considering how you implemented your solution:

  1. Check if the length of arr2 is not empty.
  2. If yes, then copy all of the items from arr1 into the new container.
  3. If no, then traverse the items of arr2.
  4. Check if n is equal to the index of the current item.
  5. If yes, then copy all of the items from arr1 and put them all starting from the current value of index into the new container.
  6. If no, copy the current item from arr2 into the new container.

This is just a high level solution of how you could possibly solve this challenge.

Try it with this console log and these function calls and read up a little on how map works. Do you see why what @arantebillywilson is saying about everything being in map is potentially your problem?

function frankenSplice(arr1, arr2, n) {
  //combine the both array
  let combo = [];
  arr2.map((item, index)=>{
    console.log('test if this is running with arrays', arr1, arr2)
    if(index == n){
      arr1.map((item)=>{
        combo.push(item)
      })
    }else if(arr2.length == 0){
      arr1.map((item)=>{
        combo.push(item)
      })
    }
    combo.push(item)
  })
  return combo
}

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

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