How to Add All Numbers in an Array Javascript

JavaScript Algorithm: Calculate Sum of All Numbers in a Jagged Array

Create a function that calculates the sum of all the numbers in a jagged array.

Erica N

We are going to write a function called sumArray that will accept an array, ar, as an argument.

For this function, you are given a jagged array. A jagged array is an array that contains a combination of numbers and other arrays. The arrays can be multiple levels deep.

The goal of the function is to find the sum of all the numbers inside the jagged array.

Example:

              let ar = [1, 2, [15, [23], [5, 12]], [100]];              //output: 158            

Next, we will find a way to make our function traverse through all the inner arrays to get our sum. One key function to make this happen is recursion.

The first thing we will do is create a variable called sum and assign it a value of 0. This is the variable we will return at the end of the function.

            let sum = 0;          

Next, we will use a for-loop to loop through our array:

            for(let el of ar){              
if (Array.isArray(el)){
el = sumArray(el);
}
sum += el;
}

Let's see what's going on inside the for-loop.

In our first if-statement, we check to see if the current iterated element is an array using the Array.isArray() method. If it is an array, we use recursion to plug that array back into our function. This will recursively go into however many levels deep into the array until we dead-end at a number.

Using our example from above, I'll create a visual of what's going on.

            [1, 2, [15, [23], [5, 12]], [100]]          

The first and second elements are numbers so they are added to our sum variable but our third element is an array.

            [15, [23], [5, 12]]          

We use recursion to go another level deep into our main array. When that array goes into the for-loop, the first element is added to our sum. But again we come across another array in the second element so we use recursion to go another level deep.

We are now three levels deep.

            [23]          

Since this array only has one item that isn't an array, we add that number into our sum and return back to the second level of our main array.

            [15, [23], [5, 12]]          

The for-loop continues to the next element which also happens to be an array.

            [5, 12]          

We recursively go into the array, loop through it, and add 5 and 12 into the sum.

The function returns back to the first level of the main array and proceeds to the next element which is [100].

            [1, 2, [15, [23], [5, 12]], [100]]          

We use recursion to return to access the second level of our main array again. Once we enter our for-loop, we add 100 to sum.

The function keeps checking each element. It is either adding a number to sum or it is going into another array. This process is repeated for each level of arrays. Once it's done checking all the elements in that array for that particular level, it continues to work backward making sure all the numbers are added.

This hopping back and forth between levels continues until the function finally reaches the first level of our main array and reaches the end.

When the for-loop ends, we return our sum.

Here is the rest of the function:

If you found this algorithm helpful, check out my other JavaScript algorithm solution articles:

How to Add All Numbers in an Array Javascript

Source: https://levelup.gitconnected.com/javascript-algorithm-calculate-sum-of-all-numbers-in-a-jagged-array-94230951d726

0 Response to "How to Add All Numbers in an Array Javascript"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel