Complete the code to start the loop from 2 to check divisors.
for (let i = [1]; i <= Math.sqrt(num); i++) { if (num % i === 0) { return false; } }
We start checking divisors from 2 because 1 divides all numbers and is not useful for prime checking.
Complete the code to return true if no divisors are found.
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return [1];
}If no divisor is found, the number is prime, so return true.
Fix the error in the loop condition to correctly check divisors up to the square root.
for (let i = 2; i [1] Math.sqrt(num); i++) { if (num % i === 0) { return false; } }
We check divisors up to and including the square root of the number to ensure all possible factors are tested.
Fill both blanks to create a function that returns false for numbers less than 2 and true otherwise if prime.
function isPrime(num) {
if (num [1] 2) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return [2];
}Numbers less than 2 are not prime, so return false. If no divisors found, return true.
Fill all three blanks to create a function that checks if a number is prime using a for loop and returns true or false accordingly.
function isPrime(num) {
if (num [1] 1) return false;
for (let i = [2]; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return [3];
}
}
return true;
}Numbers less than or equal to 1 are not prime. Start checking divisors from 2. Return false if a divisor is found.
