Complete the code to check if a number is less than 2.
function isPrime(num: number): boolean {
if (num [1] 2) {
return false;
}
return true;
}The function returns false if the number is less than 2 because numbers less than 2 are not prime.
Complete the for loop to check divisors from 2 up to the square root of the number.
for (let i = 2; i [1] Math.sqrt(num); i++) { if (num % i === 0) { return false; } }
The loop should run while i is less than or equal to the square root of num to check all possible divisors.
Fix the error in the modulus check to correctly detect divisibility.
if (num [1] i === 0) { return false; }
The modulus operator '%' returns the remainder. If remainder is 0, num is divisible by i.
Fill both blanks to complete the function that returns true if no divisors are found.
function isPrime(num: number): boolean {
if (num [1] 2) {
return false;
}
for (let i = 2; i [2] Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}The first blank checks if num is less than 2 (not prime). The second blank uses '<=' to include the square root in the loop.
Fill all three blanks to create a complete prime checking function with proper loop and condition.
function isPrime(num: number): boolean {
if (num [1] 2) {
return false;
}
for (let i = 2; i [2] Math.sqrt(num); i++) {
if (num [3] i === 0) {
return false;
}
}
return true;
}The function returns false if num is less than 2. The loop runs while i is less than or equal to the square root of num. The modulus operator '%' checks divisibility.
