What is Range error : maximum call stack size exceeded, when calling a function?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is Range error : maximum call stack size exceeded, when calling a function??

20th Apr 2018, 6:12 AM
scientist
scientist - avatar
3 Answers
+ 3
Thanks both Lord Krishna and InNoobWeTrust , you solved my problem😃😃🤗
20th Apr 2018, 9:19 AM
scientist
scientist - avatar
+ 4
Web browsers (also true in other Javascript environments like nodejs) have a limited amount of memory(stack size). If you perform large amounts of recursion or give a large number of arguments to a function it may exceed the stack size. https://stackoverflow.com/questions/22123769/rangeerror-maximum-call-stack-size-exceeded-why //Recursion demo which demonstrates the call stack size exceeded error function fact(N){ if(N <= 1){ return 1 }else{ return(N * fact(N - 1)) } } cl(fact(999999))
20th Apr 2018, 7:17 AM
Lord Krishna
Lord Krishna - avatar
+ 3
Read about "tail recursive" for a proper work around to this problem. For quick explanation, tail recursive reuse the function for each call so the program can use the stack efficiently since there is only one pointer to the function in the whole recursive loop (that's my understanding). For pure JS, tail call optimization depends on browser, for ES6, it can be achieved via strict mode. Ref: https://stackoverflow.com/a/37224563 P/S: Note that link down soon since I usually perform the cleanup of my posts and comments after a few months if I think it is of no use or not informative enough at that time. Pardon my habit! :) Update: I'm not inclined to recursive style since it may raise issues and tail call optimization is not always available so I suggest finding an iterative implementation. I'm not an expert at this but to my understanding, in the "return" line you should only return the function call (exploit only the arguments for the same effect) without any other calculation. For enabling tail call optimization, please change the code to ES6 and follow the instruction in the link I give. Hope this helps! :D
20th Apr 2018, 8:45 AM
InNoobWeTrust
InNoobWeTrust - avatar