Problem with recursion, the function is called infinite times

Hi there, I am making a web based game famously known as minesweeper.

HOW THIS SHOULD WORK => If you clicked on any tile containing a mine, game over, otherwise a number (number =total no of mines surrounding it) is shown and if there is no mine surrounding the tile, then it does the same thing with the surrounding tile , checks it, if a number then show it, if not the next surrounding tile. After a point of time, the code should stop working, until and unless any other tile is clicked.

HOW IT’S WORKING=> Everything is working as stated above but instead of stopping at some time, the function which uses recursion is called for an infinite times, which should not happen.

link to codepen

Can you specify which function in question is recursive? I see your github but the functions do not exhibit a recursive pattern. Usually these functions call themselves with smaller params size and have a conditional termination statement. Why don’t you put this on codepen since the codebase is small and be easy to demo? If you’re gonna use a lot of if statements, better to use a switch instead.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

For recursion, I have used two functions( showNumber and showSurroundingNumber).
It goes like this showNumber=>showSurroundingNumber=>showNumber and so on, if the condition is getting fulfilled.

I wouldn’t categorize this as a recursive function in the truest sense as a recursive function calls itself, not another function.
To debug your issue, if showNumber is calling showSurroundingNumber back and forth, you need to make sure either function can terminate somehow. Since you’re experiencing an endless loop, that means there is a case which isn’t being caught to terminate. An endless loop is not too hard to debug, just log your param i in both functions and see what condition isn’t being caught.
Hint: there is no else statement in showSurroundNumbers

2 Likes

Logging param i helped me check where the loop became endless. Thanks for the advice.

1 Like

one more help here’s the link