1

I want to sum the total of a multi dimensional array in JavaScript however it doesn't provide the right value because the nested array concatenates with the sum. Below is my code:

    var arr = [1, 2, 3, [4, 3], [10, 50], 98, 100];

    function recursion(array, length = 0) {
        if (array.length === length) return 0;

        if (Array.isArray(array[length])) {
            recursion(array[length]);
        }
        console.log(array[length]);

        return array[length] + recursion(array, length + 1);
    }

    console.log(recursion(arr));

The error can be seen in the below screenshot which is the console of google chrome. enter image description here

3
  • Well, the first problem you have is that JavaScript doesn't support default function arguments. (At least not in the manner you have in your code.) See MDN Commented Mar 12, 2017 at 21:27
  • 1
    If recursion is not required, you could accomplish this much more simply using concat and reduce: [].concat(...arr).reduce((a, b) => a + b) Commented Mar 12, 2017 at 21:27
  • @StephenThomas Note, javascript does support default parameters. Commented Mar 12, 2017 at 21:29

5 Answers 5

2

You're missing a return:

...
if (Array.isArray(array[length])) {
    return recursion(array[length]);
}
...

P.S. I'd recommend changing the parameter name from length to something else (perhaps position) to avoid confusion with the length property of arrays.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't even need a function for this. The capability is built into Array prototypes.

const arr = [1, 2, 3, [4, 3], [10, 50], 98, 100]
const sum = [].concat(...arr).reduce((acc, curr) => acc + curr)

console.log(sum)

1 Comment

That is a fantastic solution friend. could you please tell me what is the concept of three dots (...arr) there?
1

function sum(e) {                      // take an element and return it if it's not an array or return the recursive sum if it's an array
  if(e instanceof Array) {             // if it's an array
    return e.reduce(function(s, e) {   // call sum on each item of the array and return the accumulated sum
      return s + sum(e);
    }, 0);
  }
  else                                 // else (if it's not an array) then return the item
    return e;
}

var arr = [1, 2, 3, [4, 3], [10, 50], 98, 100];

console.log(sum(arr));

Comments

1

Another way to do this is to use reduce()

var arr = [1, 2, 3, [4, 3],[10, 50], 98, 100];

function recursion(array) {
  return array.reduce(function(r, e) {
    return r += Array.isArray(e) ? recursion(e) : e
  }, 0)
}

console.log(recursion(arr));
console.log(recursion([1, [[[1, [[[2]]]]]]]));

Comments

0

Recursion is not necessary. You can use .toString(), .split(), .reduce()

var arr = [1, 2, 3, [4, 3], [10, 50], 98, 100];
var n = arr.toString().split(/,/).reduce(function(a, b) {return +a + +b});

console.log(n);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.