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)
| 11 | ***********************************************************************/ |
| 12 | |
| 13 | function 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 | /* |
nothing calls this directly
no outgoing calls
no test coverage detected