I've noticed my algorithm solutions often include ugly nested for loops when better options are available. In the example below, how can I determine the prime numbers up to the given parameter without nested loops? One constraint of the assignment was to not utilize any external functions.
function sumPrimes(num) {
var p = [];
for (var i = 2; i <= num; i++)
{
p.push(i);
for (var j = 2; j < i; j++)
{
if (i % j === 0) //not a prime
{
p.pop(i);
break;
}
}
}
return p;