I was attempting to build a program to look through an array, grab any prime numbers, and then store them in a separate array. I have tried to mix and match some code pieces, it seems to work for the most part, but will sometimes give a non-prime number (in this case 33, divisible by 3 and 11). All of the other numbers seem to be correct. Could it be something small I am missing? Any and all help is appreciated. Thank you!
var array = [33,23,5,7,10,20,30,12,37];
primes(array);
function primes(arr){
var arrayLength = arr.length;
var primeArray = [];
function primeNum(arrElement){
if (arrElement <= 1){
console.log(arrElement + " is not a valid test number.");
}
for (var x = 2; x < arrElement; x++){
if (arrElement % x === 0){
return false;
}
return true;
}
}
for (var y = 0; y <= arrayLength - 1; y++){
if(primeNum(arr[y])){
primeArray.push(arr[y]);
}
}
console.log(primeArray);
}
Output is giving: (5) [33, 23, 5, 7, 37]