MCPcopy Create free account
hub / github.com/appacademy/Module-1-Resources / sumArray

Function sumArray

w3/d2/sum-array.js:13–27  ·  view source on GitHub ↗

Write a recursive function called `sumArray` that takes an array of integers and returns the value of all the integers added together. Your array may include a mix of positive and negative integers! Examples: sumArray([1, 2, 3]); // 6 sumArray([0, 1, -3]); // -2 sumArray([1, 2, 3, 4, 5]); // 15

(arr)

Source from the content-addressed store, hash-verified

11***********************************************************************/
12
13function sumArray(arr) {
14 if(!arr.length) {
15 return 0;
16 } else {
17
18
19 // find a way to shorten the array
20 // let popped = arr.pop();
21 let shifted = arr.shift();
22 // let [first, ...rest] = arr;
23
24 return sumArray(arr) + shifted;
25 }
26 // return arr[0] + sumArray(arr.slice(1));
27}
28
29// console.log(sumArray([1, 2, 3])); // 6
30/*

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected