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