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