Beginner Series #3 Sum of Numbers

Lee·2022년 6월 19일

Algorithm

목록 보기
20/92
post-thumbnail

❓ Complementary DNA

Q. 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)

✔ Solution

function getSum(a, b, c = 0) {
  //Good luck!

  if (a <= b) {
    for (i = a; i <= b; i++) {
      c += i;
    }
    return c;
  } else if (a > b) {
    for (i = b; i <= a; i++) {
      c += i;
    }
    return c;
  }
}

//Use Math.min && MAth.max

const GetSum = (a, b) => {
  let min = Math.min(a, b),
      max = Math.max(a, b);
  return (max - min + 1) * (min + max) / 2;
}
profile
Lee

0개의 댓글