I have been working on a program that aims to identify prime numbers within an array and store them separately. While the code seems to be functioning correctly overall, there is an issue where a non-prime number (33) is incorrectly labeled as prime due to it being divisible by 3 and 11. I suspect there may be a small error in my code causing this issue. Any guidance or assistance would be greatly 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);
}
Here is the resulting output: (5) [33, 23, 5, 7, 37]