Hyebin‘s blog

Description:

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.

 

function sumTwoSmallestNumbers(numbers) {
  const sortArray = numbers.sort(function (a, b) {
    if (a > b) return 1;
    if (a === b) return 0;
    if (a < b) return -1;
  });

  let result = 0;
  for (let i = 0; i <= 1; i++) {
    result += sortArray[i];
  }
  return result
}

 

sort() 함수 공부 자료 : https://hyebin-development-blog.tistory.com/43

'Algorithm > Codewars' 카테고리의 다른 글

[Codewars/JS] RGB To Hex Conversion  (0) 2022.01.13
[Codewars/JS] Two to One  (0) 2022.01.13
[Codewars/JS] Count the number of Duplicates  (0) 2022.01.12
[codewars/JS] Sort the odd  (0) 2022.01.12
[codewars/JS] Growth of a Population  (0) 2022.01.12
profile

Hyebin‘s blog

@hyebin Lee

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그