Integer Factorization using the Trial Division Algorithm
A few comments :


Before writing any code, perform the computation manually. How would you do it for 20? You'd do something along the lines like :


let's try the first prime number : 2
is 2 a divisor of 20 ? yes, it is. 20 is 2*10 so let's add 2 to the list of divisors and let's focus on 10 now.
is 2 a divisor of 10 ? yes, it is. 10 is 2*5 so let's add 2 to the list of divisors and let's focus on 5 now.
let's consider the next prime number. It's 3. Oh, 3*3 is actually bigger than 5 so we won't be able to find any other prime factor smaller than 5 : let's add 5 to the list of divisors.
Try it yourself  with numbers such a 8, 100, 19....

Then, about your code : the first is I noticed that your loop condition is not right and you might get out of your array. Just try a value of n greater than 168 and you'll probably notice the issue.


If you want to follow the wiki page and to something like : for p in primes: if p*p &gt; n: break what you really need to do is.

// Find prime factors
var n = 180;
var primeFactors = "";
// Trial division algorithm
for (var i = 0, p=primeNumbers[i]; i &lt; primeNumbers.length &amp;&amp; p*p&lt;=n; i++, p=primeNumbers[i]) {
    while (n % p == 0) {         
            primeFactors += " " + p + " ";
            n/=p;
    }
}
if (n&gt;1)
{
    primeFactors += " " + n + " ";
}

document.getElementById("divSolution").innerHTML = primeFactors;


(Sorry for rewriting the whole code)