본문 바로가기

Dev/Algorithm

알고리즘 문제 | 프로그래머스 | JS - 평균 구하기

반응형

풀이

function solution(arr) {
    var answer = 0;
    const reducer = (accumulator, currentValue) => accumulator + currentValue;

    answer = arr.reduce(reducer)/arr.length;
    
    return answer;
}

reduce 메서드를 통해서 합계를 구한 후 배열의 숫자만큼 나누기!

 

다른 사람 풀이

function average(array){
  return array.reduce((a, b) => a + b) / array.length;
}

똑.같.다.

반응형