So, I've been thinking of just some little practice things that I could do with arrays in JavaScript. I came across the idea of combining two arrays in a "zipping" fashion (arrayA[0], arrayB[0], arrayA[1], arrayB[1]...)
and so on and so forth. Anything left over in a potential longer array would be tacked onto the end.
I've searched stackoverflow - reason I'm asking is I'm currently in introductory programming courses, so we don't really know a whole lot of "things" we can do with JavaScript. Would like to see a solution with "simple" methods if possible!
I've currently got the alternate fashion going, but I can't seem to get the last part of tacking the remaining parts of the array to the very end.
function alternatingMerge(array1, array2)
//this function will merge two different arrays in an alternating fashion
//i.e = array1[0], array2[0], array1[1], array2[1], array1[2], array2[2], ... , etc
{
var mergedArray;
var i; // while loop counter
var j; //
var k; //
var arrayLengths;
arrayLengths = array1.length + array2.length;
i = 0; //
j = 0; // ARRAY1 COUNTER
k = 0; // ARRAY2 COUNTER
mergedArray = new Array(arrayLengths);
//window.alert(mergedArray);
while (i < arrayLengths)
{
if (i%2 === 0)
{
mergedArray[i] = array2[j];
j = j + 1;
}
else
{
mergedArray[i] = array1[k];
k = k + 1;
}
i = i + 1;
}
return mergedArray;
}
I feel like it's simple stuff but some help would be appreciated!