π
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.