
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Modified version of summing an array with recursion in JavaScript
Let’s say, we are required to write a recursive function that sums all the elements of an array of Numbers but with a twist and the twist is that the recursive function we write cannot initialize any extra variable (memory).
Like we cannot use a variable to store the sum or to keep a count of the index of the array, it all has to be using what we already have.
Here’s the solution −
We already have an array and can use its first element (i.e., the element at zeroth index to hold the recursive sum).
The approach is that we repeatedly pop one element from the array and add it to the first element of the array until we are left with only one element.
When we are left with only one element, it will be the cumulative sum of the array and we return that. The code for this approach will be −
Example
const recursiveSum = arr => { if(arr.length > 1){ arr[0] += arr.pop(); return recursiveSum(arr); }; return arr[0]; }; console.log(recursiveSum([1,2,3,4])); console.log(recursiveSum([1,2,3,4,3,6,3,32,7,9,5])); console.log(recursiveSum([]));
Output
The output in the console will be −
10 75 undefined
- Related Articles
- Summing all the unique values of an array - JavaScript
- Prefix sums (Creating an array with increasing sum) with Recursion in JavaScript
- Comparing forEach() and reduce() for summing an array of numbers in JavaScript.
- Finding product of an array using recursion in JavaScript
- Summing array of string numbers using JavaScript
- Combine unique items of an array of arrays while summing values - JavaScript
- Summing up unique array values in JavaScript
- JavaScript - summing numbers from strings nested in array
- Find the middle element of an array using recursion JavaScript
- Recursion - Sum Nested Array in JavaScript
- Summing up to amount with fewest coins in JavaScript
- Using recursion to remove consecutive duplicate entries from an array in JavaScript
- How to insert an element into all positions in an array using recursion - JavaScript?
- Using recursion to remove consecutive duplicate entries from an array - JavaScript
- Array flattening using loops and recursion in JavaScript
