Skip to main content

Featured post

How to Insert line break in Text component in React Native

Hi, I was developing an application in react native.I have to handle lots of text data in this small react native application.I have used  Text component in react native many times. While working on this application,i had a requirement to display some text content with line breaks.I will explain it clearly with an example.I want to display a text as shown below  Welcome Please subscribe my channel I wrote react native code as shown below <View> <Text> Welcome Please subscribe my channel </Text> <View> When i run these lines of code,It will display text as shown below Welcome Please subscribe my channel   - instead of displaying in two lines with line break,it displays in a single line.So i have to find a solution to insert line break in Text component in React Native.After lots of research,i found a clean solution as shown below <View> <Text> Welcome{"\n"} Please subscribe my channel </Text> <View> Here ...

RangeError: Maximum call stack size exceeded in Javascript fixed with examples

Hi friends,in this article we are discussing  about a RangeError type in javascript we will cover the below points one by one 


  1.  What is RangeError: Maximum call stack size exceeded and why it comes in recursive function
  2.  How to fix RangeError: Maximum call stack size exceeded using a termination condition in the recursive function 



What is RangeError: Maximum call stack size exceeded and why it comes in recursive function


Let us write a recursive function to get sum of all positive integers less than a given number including that number

 


function foo(num) {

    return num+ foo(--num);

}

let num=10

let totalSum= foo(num);

console.log(`Total sum of all positive integers less than ${num} is ${totalSum}`)



when we run the above lines of code we will get the error as given below


 return num+ foo(--num);

    ^


RangeError: Maximum call stack size exceeded

    at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:5) 

    at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)

    at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)

    at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)

    at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)

    at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)

    at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)

    at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)

    at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)

    at foo (C:\Users\Administrator\Desktop\JS\callstackerror.js:2:17)


Node.js v18.14.0






When we examine the above code,we can understand that foo() method is called recursively without any termination condition.So call stack size is exceeded with foo() methods call and it throws the RangeError: Maximum call stack size exceeded.




How to fix RangeError: Maximum call stack size exceeded using a termination condition in the recursive function



So now let us look into to the same example and fix the Maximum call stack size exceeded by adding a termination condition.Our requirement is to get the sum of all positive integers below a number including the number also.I solved the RangeError as shown below.


function foo(num) {

    if(num<1) return 0;

    return num+ foo(--num);

}

let num=10

let totalSum= foo(num);

console.log(`Total sum of all positive integers less than ${num} is ${totalSum}`)


we will get the result  as shown below


Total sum of all positive integers less than 10 is 55





I hope you understood the reason behind RangeError: Maximum call stack size exceeded error and How to avoid the RangeError