Ponchimeow/Practice

練習十:回傳陣列總和

Opened this issue · 0 comments

請寫一個 function sum,接收一個陣列並回傳陣列中數字的總和。

sum([1, 2, 3]) 預期回傳值:6

sum([-1, 1, 2, -2, 3, -3]) 預期回傳值:0


function sum(arr) {
    let sum = 0;
    for (let i = 0; i < arr.length; i += 1) {
      sum += arr[i]
    }
    return sum;
}