Javascript code is running out of memory-Collection of common programming errors

You’re decreasing i but checking if n > 1 .

Essentially, you’ve created an infinite loop because you never decrease the value of n. The conditions within loops allow execution until they evaluate to false. In your example, the condition clause of the for loop will always evaluate to true and the loop will continue forever.

Consider:

for (var i = n; i > 1; i--){