Owennn Owennn - 5 months ago 20
Javascript Question

How to retrieve elements in an array after filtered by other array using Javascript>

just a quick question. How can I retrieve elements in an array given that the elements cannot be divided by other elements in an array? for example=

arr =[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
and
collection =[2,3,5,7]
, then the result would be
result =[11,13]


I have tried with this code , but it didn't work

for(var i=0; i<arr.length;i++){
for (var j=0; j<collection.length; j++){
if (arr[i]/collection[j] === 0){
arr.splice(i,1);
}
}
}

Answer

If you want to find Prime Number ,I got a function for you :

function getPrimes(max) {
    var sieve = [], i, j, primes = [];
    for (i = 2; i <= max; ++i) {
        if (!sieve[i]) {
            // i has not been marked -- it is prime
            primes.push(i);
            for (j = i << 1; j <= max; j += i) {
                sieve[j] = true;
            }
        }
    }
    return primes;
}
getPrimes(100) 

will return all the prime number between 0-100, just refrence to prime numbers

Comments