Sum of two lowest positive integers

Lee·2022년 7월 3일

Algorithm

목록 보기
36/92
post-thumbnail

❓ Testing 1-2-3

Q.Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 positive integers. No floats or non-positive integers will be passed.

For example, when an array is passed like [19, 5, 42, 2, 77], the output should be 7.

[10, 343445353, 3453445, 3453545353453] should return 3453455.

✔ Solution

//#my solution
function sumTwoSmallestNumbers(numbers) {
  //Code here
  return numbers
    .sort((a, b) => a - b)
    .slice(0, 2)
    .reduce((a, b) => a + b);
}

//#other solution
function sumTwoSmallestNumbers(numbers){  
  numbers = numbers.sort(function(a, b){return a - b; });
  return numbers[0] + numbers[1];
};
profile
Lee

0개의 댓글