Why ain't this solution working

Tell us what’s happening: Hey guys, I need some help with this challenge. First I know it says slice and splice and I’m not using any of that, this is my first try with the challenge so I wanted to do it like this. The problem is when I run the tests everything is fine except for the condition “All elements from the first array should be added to the second array in their original order.”. I printed the outputs and it does the right job so I can’t figure out what’s the problem. Appreciate any help

  **Your code so far**

function frankenSplice(arr1, arr2, n) {
let newArr = [];

for (let i = 0; i < arr2.length; i++) {
  if (i === n) {
    for (let j = 0; j < arr1.length; j++) {
      newArr.push(arr1[j]);
    }
  }
  newArr.push(arr2[i]);
}
console.log(newArr);
return newArr;
}

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

Challenge: Slice and Splice

Link to the challenge:

It is failing because one of the test is frankenSplice([ 1, 2, 3, 4 ], [], 0) and since the second argument has a length of zero the first loop never happens causing the returned array to be empty

Yes!! You’re right! Thank you very much, I wasn´t understanding the empty array when printing the results of the tests. Already fixed it and passed the tests. Thanks again my friend!

1 Like

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