Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Alternatively merging two arrays - JavaScript
We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays.
For example −
If the two arrays are −
const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3];
Then the output should be −
const output = [4, 2, 3, 1, 2, 6, 5, 8, 6, 9, 8, 4, 9, 3];
Example
Following is the code −
const arr1 = [4, 3, 2, 5, 6, 8, 9];
const arr2 = [2, 1, 6, 8, 9, 4, 3];
const mergeAlernatively = (arr1, arr2) => {
const res = [];
for(let i = 0; i Output
This will produce the following output in console −
[
4, 2, 3, 1, 2, 6,
5, 8, 6, 9, 8, 4,
9, 3
]
Advertisements
