Skip to content

Latest commit

 

History

History
40 lines (28 loc) · 1 KB

merge-two-arrays.md

File metadata and controls

40 lines (28 loc) · 1 KB

Merge two arrays 7 Kyu

LINK TO THE KATA - FUNDAMENTALS ARRAYS

Description

Write a function that combines two arrays by alternatingly taking elements from each array in turn.

Examples:

[a, b, c, d, e], [1, 2, 3, 4, 5] becomes  [a, 1, b, 2, c, 3, d, 4, e, 5]

[1, 2, 3], [a, b, c, d, e, f] becomes [1, a, 2, b, 3, c, d, e, f]

Points:

  1. The arrays may be of different lengths, with at least one character/digit.
  2. One array will be of string characters (in lower case, a-z), a second of integers (all positive starting at 1).

Solution

const mergeArrays = (a, b) => {
  const result = []
  const biggerArrayLength = a.length >= b.length ? a.length : b.length

  for (let i = 0; i < biggerArrayLength; i++) {
    if (a[i] !== undefined) result.push(a[i])
    if (b[i] !== undefined) result.push(b[i])
  }

  return result
}