Try to run the same code on Codepen or locally and it will work.
The FCC site limits you to a certain (large) amounts of calls or stack size or time-running or whatever. To make your algorithm work you need to make it more efficient.
What I did: try to find the pattern of the numbers that you don’t need to check and then simply skip them. You can make the algorithm a lot more efficient like that.
Good luck!