Basic Algorithm Scripting: Slice and Splice***


What is the reason why it is not making push correctly. What is my error?

function frankenSplice(arr1, arr2, n) {

let elemArr=[];
let sliceArr=arr2.slice(n);
           //console.log((sliceArr))  
 
 console.log(sliceArr.push(elemArr))

 for (let i=0;i<arr1.length;i++){
          
           elemArr.push(arr1[i])      
                 
 
                
               
                }

               
           //console.log(sliceArr)
              //console.log(elemArr)
              //console.log(sliceArr.push(elemArr))        
        }

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

I added comments to your code so you can see how it works. Maybe it’ll help.

function frankenSplice(arr1, arr2, n) {

  // create an empty array
  let elemArr = [];
  // create an array and assign to it elements from arr2
  // starting with element n
  let sliceArr = arr2.slice(n);

  // append the empty elemArr to sliceArr
  console.log(sliceArr.push(elemArr))

  for (let i = 0; i < arr1.length; i++) {
    // put all arr1 elements in the array at the end of sliceArr on by one
    elemArr.push(arr1[i])
  }
  // nothing is returned from function
}
1 Like