Problem 7: 10001st prime Bug

Tell us what’s happening:
My code gives the correct output when I’m running it from my PC.
But, when I try it from the online console of FreeCodeCamp the test runners don’t succeed.

Your code so far


function isPrime(n) {
    if (n <= 0) return false
    if (n == 2 || n == 3) return true
    if (n > 3) {
        var div = 2
        while (div < n) {
            if (n % div == 0) return false
            div += 1
        }
        return true
    }
}

function nthPrime(n) {
    var primeNums = []

    for (var i = 2; primeNums.length < n; i++) {
        if ((isPrime(i))) primeNums.push(i)
    }

    console.log("Result ", primeNums[primeNums.length - 1])
    return primeNums[primeNums.length - 1]
}

var answer = nthPrime(1000)
console.log(answer)

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
Coding Interview Prep -> Project Euler -> Problem 7

You are probably triggering the infinite loop protection, you need a code with better performance

You are checking if a number is prime checking if it is divisible by each single number lower than it

This is not sonething you can brute force that easily, you will need to thing an other way