I am trying to filter an array of random numbers to pull out primes.
I have a working function (isPrime
) that appears to return true
or false
correctly but every number in the array is being "filtered" into the array returned by the filter.
I looked at the documentation for filter and watched some videos but I haven't figured it out, I think it may have to do with the function call inside of the filter. Can you tell me where my error is?
numArray = [];
for(i=0; i<1000; i++){
numArray.push(Math.random(1000));
}
const isPrime = (num) => {
for(i=2;i<num;i++){
if(num % i === 0){
return false;
}
}
return true;
}
const results = numArray.filter(value => isPrime(value));
for(num in results){
console.log(num);
console.log(isPrime(num)) //added to demonstrate function is working and should return false to the filter
}