Algorithm/Codewars
[codewars/JS] Sum of two lowest positive integers
hyebin Lee
2022. 1. 11. 22:21
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