문제 설명
정수 배열 numbers가 매개변수로 주어집니다. numbers의 원소의 평균값을 return하도록 solution 함수를 완성해주세요.
제한 사항
- 0 ≤ numbers의 원소 ≤ 1,000
- 1 ≤ numbers의 길이 ≤ 100
- 정답의 소수 부분이 .0 또는 .5인 경우만 입력으로 주어집니다.
입출력 예
numers | result |
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | 5.5 |
[89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] | 94.0 |
입출력 예 설명
입출력 예 #1
- numbers의 원소들의 평균 값은 5.5입니다.
입출력 예 #2
- numbers의 원소들의 평균 값은 94.0입니다.
solution.js
function solution(numbers) {
const sum = numbers.reduce((acc, cur) => acc + cur, 0);
return sum/numbers.length;
}
array에 있는 값을 전부 더해야하다보니 reduce를 사용
사용법은 공식 사이트 참고*
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
Array.prototype.reduce() - JavaScript | MDN
The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the a
developer.mozilla.org
solution.py
def solution(numbers):
return sum(numbers) / len(numbers);
python3 더쉽ㄷ..

'코딩테스트 연습 > lv.0' 카테고리의 다른 글
아이스 아메리카노 (0) | 2023.06.03 |
---|---|
옷가게 할인 받기 (0) | 2023.05.15 |
피자 나눠 먹기 (3) (0) | 2023.05.09 |
피자 나눠 먹기 (2) (0) | 2023.05.01 |
피자 나눠 먹기 (1) (0) | 2023.04.27 |