Challenge - 5 Problems
Prime Checker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Prime Check for 11
What is the output of the following JavaScript code that checks if 11 is prime?
DSA Javascript
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(11));Attempts:
2 left
💡 Hint
Remember, 11 is a prime number because it has no divisors other than 1 and itself.
✗ Incorrect
The function checks divisors from 2 up to the square root of 11. Since none divide 11 evenly, it returns true.
❓ Predict Output
intermediateOutput of Prime Check for 1
What is the output of the following JavaScript code that checks if 1 is prime?
DSA Javascript
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(1));Attempts:
2 left
💡 Hint
Recall that 1 is not considered a prime number.
✗ Incorrect
The function returns false immediately because 1 is not prime by definition.
❓ Predict Output
advancedOutput of Prime Check for 49
What is the output of the following JavaScript code that checks if 49 is prime?
DSA Javascript
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(49));Attempts:
2 left
💡 Hint
Check if 49 has any divisors other than 1 and itself.
✗ Incorrect
49 is divisible by 7, so the function returns false.
❓ Predict Output
advancedOutput of Prime Check for 2
What is the output of the following JavaScript code that checks if 2 is prime?
DSA Javascript
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(2));Attempts:
2 left
💡 Hint
2 is the smallest prime number.
✗ Incorrect
The loop does not run because 2 <= sqrt(2) is true (sqrt(2) ~1.414), but the loop condition is i <= Math.sqrt(num), so i=2 <=1.414 is false, so loop does not run, so function returns true.
❓ Predict Output
expertOutput of Prime Check for 0
What is the output of the following JavaScript code that checks if 0 is prime?
DSA Javascript
function isPrime(num) {
if (num <= 1) return false;
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) return false;
}
return true;
}
console.log(isPrime(0));Attempts:
2 left
💡 Hint
0 is not a prime number by definition.
✗ Incorrect
The function returns false immediately because 0 is less than or equal to 1.
