Skip to content

Latest commit

 

History

History
45 lines (34 loc) · 987 Bytes

every-possible-sum-of-two-digits.md

File metadata and controls

45 lines (34 loc) · 987 Bytes

Every possible sum of two digits 7 Kyu

LINK TO THE KATA - ALGORITHMS

Description

Given a long number, return all the possible sum of two digits of it.

For example, 12345: all possible sum of two digits from that number are:

[ 1 + 2, 1 + 3, 1 + 4, 1 + 5, 2 + 3, 2 + 4, 2 + 5, 3 + 4, 3 + 5, 4 + 5 ]

Therefore the result must be:

[ 3, 4, 5, 6, 5, 6, 7, 7, 8, 9 ]

Solution

const digits = num => {
  const digits = num
    .toString()
    .split('')
    .map(character => Number(character))
  const digitsLength = digits.length
  const result = []

  for (let i = 0; i < digitsLength; i++) {
    const currentNumber = digits[0]
    digits.shift()
    let sumArray = digits.map(number => number + currentNumber)
    result.push(sumArray)
  }

  return result.reduce((a, b) => a.concat(b))
}