Home > Enterprise >  Maximum Call Stack Size Exceeded During a Recursive function with setTimeout Call
Maximum Call Stack Size Exceeded During a Recursive function with setTimeout Call

Time:10-11

I'm trying to execute the following function using Fidler but still not getting the expected results. Basically, I have a deeply nested array that I would like to iterate through and get the sum. I have gone through the SO question but still can't see what I'm doing wrong.

The code is as follows:

EDIT

Thanks for the responses but the reason I was trying to use setTimeout() because the elements are many. See the full code below which I'm trying in Fiddler

const ElementsCount = 10000; //If I change this to 1000 its fine, when 10000 or more it gives error

const createDeeplyNestedArray = (ElementsCount) => {
      let retval = [1];
      for (let i = 0; i < ElementsCount - 1; i  ) {
        retval = [1, retval];
      }
      return retval;
    };

const deeplyNestedArray = createDeeplyNestedArray(ElementsCount);
let sum = 0;

function sumArray(deeplyNestedArray) {
  let sum = 0;
  for (const entry of deeplyNestedArray) {
    if (typeof entry === 'number') {
      sum  = entry;
    } else {
      sum  = sumArray(entry);
    }
  }
  return sum;
}
var res = sumArray(deeplyNestedArray);
console.log(res); //Expected 10000 but getting maximum call stack exceeded

CodePudding user response:

You don't need to use setTimeout at all. In fact, there is an easier way to perform the sum:

  1. Start sum with 0
  2. Iterate through a received array (from the first positional argument).
    • If the entry is a number, add it to the sum
    • If the entry is an array, recurse but adding the return function itself to the sum

In code it will look something like this:

function sumArray(arr) {
  let sum = 0;
  for (const entry of arr) {
    if (typeof entry === 'number') {
      sum  = entry;
    } else {
      sum  = sumArray(entry);
    }
  }
  return sum;
}

See proof-of-concept below, with optimisation using ternary operator:

const deeplyNestedArray = [1, [1, [1, [1, [1, [1, [1, [1, [1, [1]]]]]]]]]]

function sumArray(arr) {
  let sum = 0;
  for (const entry of arr) {
    sum  = typeof entry === 'number' ? entry : sumArray(entry);
  }
  return sum;
}

const result = sumArray(deeplyNestedArray);
console.log("sum", result);

CodePudding user response:

Basic answer: setTimeout is not waiting for calculation of sum, so after calling of sumArray - action is done inly one time. And after this - you are receiving answer where sum is 1.

const deeplyNestedArray = [1, [1, [1, [1, [1, [1, [1, [1, [1, [1]]]]]]]]]]
var sum = 0;

function sumArray() {
   let element = deeplyNestedArray.shift();

    if(element){
        if (Number.isInteger( element)) sum  =  element;
        else deeplyNestedArray.push(...element);

        sumArray();
    }
  return sum;
}

const result = sumArray();
console.log("sum",result);

  • Related