Slice and Splice and forEach()

Tell us what’s happening:

Hey campers!

Can you please explain, why in this task forEach() method loops from the end and not from the beginnig of an array, so that I have to reverse() the array to get correct results.

Thank you!

Your code so far


function frankenSplice(arr1, arr2, n) {
  
  let modif = arr2.slice(0);

  arr1.reverse().forEach(arg=> modif.splice(n, 0, arg)); 
 
    arr1.reverse(); 
        
    return modif;
}  


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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice

you are reversing it in both of these lines…
So you reverse arr1 then apply forEach then apply reverse.

Is that what you wanted to do?

Using reverse() method before forEach() is giving you such result. Remove both reverse() methods and it will work well.

Hey!

without applying the reverse() your result will look like [4, 3, 2, 1, 5, ] instead [4, 1, 2, 3, 5] which is not what it should be. Moreover, your initial array should remain the same(intact), this is why you apply reverse() second time.

My question was: why without applying reverse() first time: arr1.reverse().forEach(arg=> modif.splice(n, 0, arg)); like arr1.forEach(arg=> modif.splice(n, 0, arg)); => the output is [4, 3, 2, 1, 5, ] instead [4, 1, 2, 3, 5]

Hey!

How it will work well, if without reversing array the result of arr1.forEach(arg=> modif.splice(n, 0, arg)) => [4, 3, 2, 1, 5, ] and you need [4, 1, 2, 3, 5]

Thanks.

I’m having a hard time understanding your logic or your last response.

I believe the problem is very simple (you can search the forum for how others have solved it if you are really stuck).
You don’t need one reverse or two reverse.

You just need to understand what is being asked of you.

Take one array (arr1) and insert it into the second array (arr2) without changing either arr1 and arr2.
therefore, you need to
1- make a copy of arr2 to be able to do the insertion (use slice)
2- use splice on the copy you made to insert the contents of arr1 into it at the correct spot
3- return the new array

done.

The question is NOT how to solve this problem< it is solved with the code in my first post.

The question is WHY this line of code: arr1.forEach(arg=> modif.splice(n, 0, arg)) gives you the following result : [4, 3, 2, 1, 5, ] when it should be [4, 1,2,3, 5].

function frankenSplice(arr1, arr2, n) {

let modif = arr2.slice(0);

arr1.forEach(arg => modif.splice(n, 0, arg));


return modif;

}

console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1)); // outp : [4, 3, 2, 1, 5, ] and NOT [4, 1,2,3, 5].

If I’m to look at the code right now, what you are doing is splicing into a copy arr2. Now I’m sure that much is already obvious to you.

But they key importance here is n. n is 1 in this case. Let’s go through step by step what’s happening.

the value modif (using spaces instead of commas, since i’m lazy):

iteration 1: [4  1  5  6]
iteration 2: [4  2  1  5  6]
iteration 3: [4  3  2  1  5  6]

Key takeaway here is, what index 1 after each iteration? Remember, n does not change with each passing iteration because you make no adjustments to n during each pass.

1 Like

I put the code thru repl.it and found that modif is equal to [4,1,2,3,5,6] after the foreach line

YES! That is what I am talking about! It solves the issue. Because you use reverse(). I have hard time understanding why without reversing -first time: arr1.reverse(), forEach()method is looping from end to beginning of the array

Did you see my post?

ok then! I’m happy for you.
But if it is doesn’t offend you for me to suggest this, i think you should go back and solve this the way it was intended.
The code right now is just not up to the specification given by the exercise (even though it works) and it suggests you have missed the point of this exercise.
(ie. try to solve this one without the forEach)

Just now, THANK YOU!Finally!)))

Good. Hopefully it clears up why you had to do the reverse. The more efficient ways shouldn’t require you to reverse, so you might want to rethink the challenge. If you remember the spread operator from ES6, you would have an even easier time I believe.

Of course, I will rethink it))! Again, thanks for answering the question!

No problem. Good luck!

Oh, not it’s ok. Thanks for your time.