Basic Algorithm Scripting: Slice and Splice - looping issue?

Tell us what’s happening:
Below is my code. I suspect my problem is with how I am assigning the variable newArray and writing the loop code. Any feedback would be appreciated.

Your code so far


function frankenSplice(arr1, arr2, n) {
var array2 = arr2.slice(0);
var array1 = arr1.slice(0);
var newArray = 
  for (var i = 0; i < array1.length; i++) {
    array2.splice(n, 0, array1[i]);
  }
return newArray;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36.

Challenge: Slice and Splice

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

Is that the full code or is something missing? What are you assigning to newArray?

try running it on a different editor than freecodecamp, so you also get the syntax errors.

To do that, copy your code. Open an editor for JavaScript (my suggestion is repl.it), write in the first line "use strict" (quote included), and below it paste your code. Try running it.
In this way if it doesn’t run, it says why, clearly stating the fatal errors that stop the running of the code.

That is the full code I have so far. I am assigning the loop to the variable. Clearly this is wrong, but that’s what I’ve got right now.

Ok thank you.

I just ran the code in repl.it and it gave me the error below (and I have no idea what it means!). Any ideas?

home/runner/ReasonableBowedLoaderprogram/index.js:6
for (var i = 0; i < array1.length; i++) {
^^^

SyntaxError: Unexpected token for

It means the for keyword is not an expected token after an assignment (=) operator.

It’s not valid syntax. I’m not even sure how that would work. What it is you expect to get assigned to the variable when doing that? A for loop doesn’t return anything, it’s not like a function or an array method.

Ok, here is some updated code (which doesn’t work either but I’m hoping I’m moving in the right direction:

function frankenSplice(arr1, arr2, n) {
  var array2 = arr2.slice(0);
  var array1 = arr1.slice(0);
  var newArray = [];
  for (var i = 0; i < array1.length; i++) {
      newArray = array2.splice(n, 0, array1[i]);
    }
    return newArray;
}

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

This code…

newArray = array2.splice(n, 0, array1[i]);

…will just keep assigning the element that was removed, if something actually is removed (see the splice docs for the possible return values), from array2 to newArray on each loop iteration. You are not “building” a new array just reassigning a value on each loop iteration.

You should also read the documentation for splice and make sure you are using it correctly.

Ok thanks for the tip. I’ve read the documentation for splice several times but I will review it again.

You may also want to consider in what order the elements are added. It may be a bit counterintuitive but what you have to remember is the start location is fixed so each number will be added to that location.

Here is some pseudo-code first, let say I’m adding Array2 to Array1 starting at position 2

Array1 = 1, 2, 6

Array2 = 3, 4, 5

start at position 2 add the number 3

1, 2, 3, 6

start at position 2 add the number 4

1, 2, 4, 3, 6

start at position 2 add the number 5

1, 2, 5, 4, 3, 6
const array1 = [1, 2, 6];
const array2 = [3, 4, 5];

for (let i = 0; i < array2.length; i++) {
  array1.splice(2, 0, array2[i]);
}

console.log(array1);
// [ 1, 2, 5, 4, 3, 6 ]

Also, as you have already made copies of the arrays, you don’t really need to worry about mutating them (changing the original arrays). So you do not need newArr, you can just splice on array2 and return that from the function.

Thanks for the pseudo code and explanation. I did not think about the fact that at each iteration the array item is being added at position 2.

The example code I gave is adding it at position 2.

Your code (the challenge code) is adding it at position n, that is whatever number was passed to the n parameter.

yes, got it, thanks. I should have said each item will be added at position n.

Thanks, I used the reverse method on array1 and that did the trick.

Did you pass the challenge? If so, good job!

You can also loop backward, instead of reversing the array. You just reverse the logic of the loop. That is, start the index at array.length - 1 and check that the index is greater than or equal to zero, then decrement the index i--

(let i = someArray.length - 1; i >= 0; i--)

Yes, passed the challenge! Thanks. I tried looping backward but couldn’t get it to work, and I see your code is different than what I had. (I had i = array.length and I had i > 0).

Late reply, sorry.

I wanted to mention that instead of reversing or looping backward. You can also just increment the start position by the index. I forgot and just never got around to it, my bad.

array2.splice(n + i, 0, array1[i])