πŸ“š

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.