📚

Array Sum

Array Sum
쉬움 배열 +10pts

Problem

Write a function that returns the sum of all numbers in an array.

Examples

Input: sum([1, 2, 3, 4, 5])
Output: 15
💡 1 + 2 + 3 + 4 + 5 = 15

Explanation

이 문제는 배열의 모든 요소를 순회하며 합계를 구하는 방법을 학습합니다. **reduce 메서드**는 배열을 하나의 값으로 축소하는 데 가장 적합한 도구입니다. **reduce 메서드의 동작 원리** `reduce((accumulator, currentValue) => ..., initialValue)` - accumulator(acc): 누적된 결과값 - currentValue(cur): 현재 처리 중인 요소 - initialValue: 초기값 (여기서는 0) 배열 [1, 2, 3, 4, 5]의 경우: 1. acc=0, cur...

View detailed explanation →

Key Concepts

reduce 메서드 배열 순회 누적값 계산 초기값 설정
Time: O(n) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.