Write a JavaScript function that checks whether a given number is prime or not.
function isPrime(number) {
if (number <= 1) {
return false;
}
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
return false;
}
}
return true;
}
// Test the function
const num = 29;
const isNumPrime = isPrime(num);
console.log(isNumPrime);
Comments
Post a Comment