1

My goal is to make a function that moves the list items from ar1 far enough forward that I can fit ar2 in the space that I've made. (#1) I am taking the indices of ar1 copying them and moving them forward, then I am putting the indices of ar1 where the item I just copied is. (#2) I have tested my loop without my conditions and the loop is valid. The problems seem to be with my conditions (#1), neither of them will stop the loop (#2) independently, so I concluded that there was no reason to make a second loop.

No where in the script have I changed the length of ar2 or interrupted the counter. The condition is never met.

var firstArray = [];

for (var h = 0; h < 10; ++h) {
    firstArray[h] = h;
}

secondArray = firstArray;
secondArray.reverse();

function joinArray(ar1, ar2, index) {
/* #2 */
    for (var i = 0; i < ar2.length; i++) {
/* #1 */
        ar1[index+ar2.length+i] = ar1[index+i];
        ar1[index+i] = ar2[i];
    }
    console.log(ar1);
}
joinArray(firstArray, secondArray, input);
Community
  • 1
  • 1
Enzo01
  • 11
  • 1
  • 2
    `secondArray = firstArray` makes both variables refer to the exact same array. Changing the other variable changes them both. – JJJ Feb 21 '16 at 19:29
  • 1
    See [Copying array by value in JavaScript](http://stackoverflow.com/questions/7486085/copying-array-by-value-in-javascript) – JJJ Feb 21 '16 at 19:30
  • That looks like the answer – Brian White Feb 21 '16 at 19:44

0 Answers0