[프로그래머스] 없는 숫자 더하기

·2023년 9월 27일
0

0부터 9까지의 숫자 중 일부가 들어있는 정수 배열 numbers가 매개변수로 주어집니다. numbers에서 찾을 수 없는 0부터 9까지의 숫자를 모두 찾아 더한 수를 return 하도록 solution 함수를 완성해주세요.

내 풀이

function solution(numbers) {
    var answer = 0;
    for(let i = 0; i < 10; i++) {
        if(!numbers.includes(i)) {
            answer += i
        }
    }
    return answer;
}

다른 사람의 풀이

function solution(numbers) {
    return 45 - numbers.reduce((cur, acc) => cur + acc, 0);
}
profile
개발자가 되는 과정

0개의 댓글