Implementation of alternate merge from two arrays in JavaScript


Took 18 minutes and 18 seconds to make this JavaScript function in which both arrays should be of the same length. Here is the code snippet.

Function:
arr1 = ['bla', 'bla', 'bla'];
arr2 = ['ha', 'ha', 'ha'];
arr3 = [];

function alternateMerge(arr1, arr2) {
for (var i = 0; i < arr2.length; i++) {
arr3.push(arr1[i]);
arr3.push(arr2[i]);
}
}

alternateMerge(arr1, arr2);

document.write(arr3);

Result:
bla,ha,bla,ha,bla,ha

No comments:

Post a Comment

Coding for Kids

What is coding? In coding we build a program to do a specific task for us. Code: A code is a set of computer instructions and when you will ...