I am having stack overflow error while calling the function in the for loop
verify your code with this: