sumTwoSmallestNumbers

samuel Jo·2023년 6월 13일
0

codewars

목록 보기
23/46

// 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.

배열에서 가장 작은 수 두개를 합치는 문제.
sort함수로 오름차순 정리해서 더하면 된다.

function sumTwoSmallestNumbers(numbers){
    numbers.sort(function(a,b){
        // 오름차순으로 정리.
        return a-b;
    })
    //0번째인덱스 가장작고, 1번째인덱스 그다음 작은 수
    return numbers[0]+numbers[1];
    
}
console.log(sumTwoSmallestNumbers([10, 343445353, 3453445, 3453545353453]));
//3453455
profile
step by step

0개의 댓글