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

GITEX Dubai Talk by Saion Gupta and Saiasmi Gupta

Good morning, everyone. I'm Saion Gupta, the Founder of 10xCoderKids and the Guinness World Record holder for the youngest computer prog...