javascript get prime numbers code example
Example 1: javascript find prime number
function test_prime(n){
if (n===1) {
return false;
}else if(n === 2){
return true;
}else{
for(var x = 2; x < n; x++){
if(n % x === 0){
return false;
}
}
return true;
}
}
console.log(test_prime(37));
Example 2: find prime number function javascript es6
const isPrime = num => {
const boundary = Math.floor(Math.sqrt(num));
for (var i = 2; i <= boundary; i++) if (num % i === 0) return false;
return num >= 2;
};
console.log(isPrime(11));
console.log(isPrime(17));
console.log(isPrime(8));