Skip to content

Latest commit

 

History

History
46 lines (32 loc) · 1.11 KB

beginner-series-3-sum-of-numbers.md

File metadata and controls

46 lines (32 loc) · 1.11 KB

Beginner Series #3 Sum of Numbers 7 Kyu

LINK TO THE KATA - FUNDAMENTALS ALGORITHMS

Description

Given two integers a and b, which can be positive or negative, find the sum of all the integers between and including them and return it. If the two numbers are equal return a or b.

Note: a and b are not ordered!

Examples (a, b) --> output (explanation)

(1, 0) --> 1 (1 + 0 = 1)
(1, 2) --> 3 (1 + 2 = 3)
(0, 1) --> 1 (0 + 1 = 1)
(1, 1) --> 1 (1 since both are same)
(-1, 0) --> -1 (-1 + 0 = -1)
(-1, 2) --> 2 (-1 + 0 + 1 + 2 = 2)

Your function should only return a number, not the explanation about how you get that number.

Solution

const getSum = (a, b) => {
  if (a === b) return a

  const minNumber = Math.min(a, b)
  const maxNumber = Math.max(a, b)

  const arrayNumbers = []

  for (let i = minNumber; i <= maxNumber; i++) {
    arrayNumbers.push(i)
  }

  return arrayNumbers.reduce((acc, cur) => acc + cur)
}