Quick question over an algorithm problem

Hi! At the time, I’m in this challenge of the javascript certificate:

You are given two arrays and an index.

Copy each element of the first array into the second array, in order.

Begin inserting elements at index n of the second array.

Return the resulting array. The input arrays should remain the same after the function runs.

So my solution was this:

function frankenSplice(arr1, arr2, n) {
  let secondArr = arr2.slice()
  for (let values of secondArr) {
    if (secondArr.indexOf(values) === n) {
      secondArr.splice(n, 0, ...arr1)
  return secondArr

I know that’s not the best way to do it, it’s just that I focus on solving the problem first and then work my way backwards to make it more redable/efficient. Now the thing is, the above function passed all the test except for the condition that: “All elements from the first array should be added to the second array in their original order.”
Wich is find a bit weird because all the test cases were positive. I eventually reduced the code to this:

function frankenSplice(arr1, arr2, n) {
  let secondArr = arr2.slice()
  secondArr.splice(n, 0, ...arr1)
  return secondArr

And this worked…
Clearly it has something to do with my for…of loop, but just wanted to know why. Thanks beforehand!

A few things.

  • If you only want to perform an action once, there is no need for a loop. Loops are for when you want to perform the same logic over and over, for each item in a collection. Notice how you don’t actually need to use values in your logic.
  • You are mutating the array while you are looping over it. That can get you all sorts of wacky logical errors, including infinite loops.
  • The if (secondArr.indexOf(values) === n) { is a bit perplexing to me. I would guess that the intention was to only do the splice once, but from what you’ve shared it doesn’t seem like there’s any guarantee that every value will be unique, so you could perform that action multiple times. This is also a case where the fact that you are mutating the array as you go could cause problems.

Yeap! This was me not understanding the .splice method well enough. But was wondering if there were any other bad stuff in the first example. Thanks!

also… what happens if secondArr is []?

That wasn’t a test case in the problem, but when any of the arrays are empty and the num is one, the function should return 0.
Don’t get me wrong, I know the first example is some really nasty, spaghetti and cringe worthy code😅
It’s just that I’m fairly new to this, so my approach is to break the problem into little pieces and come up with solutions for each one. I usually end up realizing that I’ve overcomplicate things and the answer is a lot more simpler lol

try to add console.log({arr1, arr2, n}) inside the function and run the tests, one has an empty array as input

the function should still return an array, nowhere is a number mentioned as output

what happens is that the loop doesn’t execute, the elements of arr1 are not added, and you just return [] without the added elements

1 Like

You’re right! My bad, I don’t know why I was mixing the test cases with the ones of another problem that comes after this one…
I know it is some wacky logic, I was just curious to know why I was getting the expected output but still marked as if “arr1 wasn’t being added in order” (without thinking arr2 could be an empty array😅)