David G
David G

Reputation: 96835

Code to display all prime numbers not working in JavaScript?

I'm trying to display all the prime numbers up to 10 and it isn't working. Can you see what I did wrong?

function findPrimeNumbers() {
    var count = 10,
        primes = [];
    for (var i = 0; i <= count; i++) {
        if (count / i === 1 || count) primes.push(i);
        else continue;
        count -= 1;
    }
    for (var i = 0, len = primes.length; i < len; i++) return primes[i];
}

console.log(findPrimeNumbers());

It only returns 0 in the console.

Upvotes: 0

Views: 2664

Answers (6)

Mayank_VK
Mayank_VK

Reputation: 17

function PrimeCheck(n){      //function to check prime number
 for(i=2;i<n;i++){
  if(n%i==0){
   return false
  }
}
return true;
}
 function print(x){                   //function to print prime numbers
var primeArray=[];
for(j=2;j<x;j++){
if(PrimeCheck(j)==true){             
  primeArray.push(j);
}
}
 console.log(primeArray);
}
print(10);                          //[2,3,5,7]

Upvotes: 0

Brock Adams
Brock Adams

Reputation: 93533

Here's about the simplest way to generate primes. Note that there are more efficient methods, but they are harder to understand.

function findPrimeNumbers (count) {
    var primes              = [];

    for (var J = 2;  J <= count;  J++) {
        var possPrime       = true;

        for (var K = 2, factorLim = Math.sqrt (J);  K <= factorLim;  K++) {
            if (J % K == 0) {
                possPrime   = false;
                break;
            }
        }
        if (possPrime)
            primes.push (J);
    }
    return primes;
}

console.log (findPrimeNumbers (10) );


This yields all the primes <= 10:

[2, 3, 5, 7]


See Wikipedia for an explanation.

Upvotes: 2

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25465

for (var i = 0, len = primes.length; i < len; i++) return primes[i];

Here you are return just the first element of the array. I think you meant something like this

var retstr = "";
for (var i = 0, len = primes.length; i < len; i++)
{
    //To improve str format
    if(i == len-1)
        retstr += primes[i];
    else
        retstr += primes[i] + ", ";
}

return retstr;

Hope this helps.

Upvotes: 0

baretta
baretta

Reputation: 7595

And you are returning from the function in the last for loop. Remove that for loop, just return the array.

Upvotes: 0

Jonathan Hall
Jonathan Hall

Reputation: 79684

You don't say how it's not working, but the first thing that comes to my attention is that you're incrementing i, while at the same time decrementing count, so i will never get all the way to 10.

Also, count / i will cause a divide-by-zero error on the first iteration as it's written (unless Javascript magically handles that case in some way I'm not familiar with).

Then you "loop" through your return values--but you can only return once from a function, so of course you're only going to return the first value.

Upvotes: 0

CatchingMonkey
CatchingMonkey

Reputation: 1391

if (count / i === 1 || count / i === count)

Upvotes: 0

Related Questions