Skip to content

Commit 2645455

Browse files
author
juanantonioledesma
committed
Add "Merge two arrays" kata
1 parent c30efe0 commit 2645455

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

7-kyu/merge-two-arrays.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<h1>Merge two arrays <sup><sup>7 Kyu</sup></sup></h1>
2+
3+
<sup>
4+
<a href="https://www.codewars.com/kata/583af10620dda4da270000c5">
5+
<strong>LINK TO THE KATA</strong>
6+
</a> - <code>FUNDAMENTALS</code> <code>ARRAYS</code>
7+
</sup>
8+
9+
## Description
10+
11+
Write a function that combines two arrays by alternatingly taking elements from each array in turn.
12+
13+
Examples:
14+
15+
```
16+
[a, b, c, d, e], [1, 2, 3, 4, 5] becomes [a, 1, b, 2, c, 3, d, 4, e, 5]
17+
18+
[1, 2, 3], [a, b, c, d, e, f] becomes [1, a, 2, b, 3, c, d, e, f]
19+
```
20+
21+
Points:
22+
23+
1. The arrays may be of different lengths, with at least one character/digit.
24+
2. One array will be of string characters (in lower case, a-z), a second of integers (all positive starting at 1).
25+
26+
## Solution
27+
28+
```javascript
29+
const mergeArrays = (a, b) => {
30+
const result = []
31+
const biggerArrayLength = a.length >= b.length ? a.length : b.length
32+
33+
for (let i = 0; i < biggerArrayLength; i++) {
34+
if (a[i] !== undefined) result.push(a[i])
35+
if (b[i] !== undefined) result.push(b[i])
36+
}
37+
38+
return result
39+
}
40+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ JavaScript katas on Codewars are programming challenges that help you improve yo
8080
- **[Homogenous arrays](./7-kyu/homogenous-arrays.md)**
8181
- **[L2: Triple X](./7-kyu/l2-triple-x.md)**
8282
- **[Letterbox Paint-Squad](./7-kyu/letterbox-paint-squad.md)**
83+
- **[Merge two arrays](./7-kyu/merge-two-arrays.md)**
8384
- **[Return the closest number multiple of 10](./7-kyu/return-the-closest-number-multiple-of-10.md)**
8485
- **[Sort Numbers](./7-kyu/sort-numbers.md)**
8586
- **[Sum of two lowest positive integers](./7-kyu/sum-of-two-lowest-positive-integers.md)**

0 commit comments

Comments
 (0)