How to display the prime numbers that are less than the (limit) in ascending order by using the function showPrimes() and parameter (limit) it

script type="text/javascript">
        function showPrimes(limit){
            for(let number= 2; number<= limit; ++number){
                let isPrime = true;


Above r the code I am currently working on so far. Not sure how to continue.


It looks like you have Codecademy Pro, so you should be able to access this interactive article on the Sieve of Eratosthenes algorithm for finding prime numbers

The article outlines a method for finding primes, has exercises along the way to build on it, and even concludes with optimization strategies to cut down the computing cost of running it.

If you have any specific questions after you look this over, then be sure to post again.

1 Like

Just to show it can be done with an elementary approach,

primes = new Array(100 + 1)
n = Math.floor(Math.sqrt(primes.length) + 1)
primes[0] = false
primes[1] = false
for (let i = 2; i < n; i++) {
  for (let j = i + i; j < primes.length; j += i) {
    primes[j] = false
prime = []
primes.forEach(function (x, i) {
  this.push(x && i || 0)
}, prime)
p = prime.filter(x => x !== 0)

Maybe not the benchmark approach but we got what we’re after, right? We build on our successes and learn from our failures. It’s all uphill.

1 Like

That’s the Sieve of Eratosthenes algorithm outlined in the article :laughing:

1 Like

But we built it intuitively, from the model given. That’s more important. Being able to envision a model and then build from it is working toward a goal.

Technically we know that we don’t have to iterate a sequence. Just a list of the primes less than or equal the square root of N is enough. That takes some memoizaiton, though, and is another path on the road to optimizing this algorithm.

A sieve is not enough, we need an object. That becomes evident.