Hint: Use Euclidean algorithm steps to find GCD quickly [OK]
Common Mistakes:
Confusing modulus with division
Not updating variables correctly in loop
Returning wrong variable at end
4. Find the error in this JavaScript function that tries to check if a number is prime:
function isPrime(n) {
if (n <= 1) return false;
for (let i = 2; i < n; i++) {
if (n % i = 0) return false;
}
return true;
}
medium
A. The loop should start from 1 instead of 2
B. The comparison operator in the if condition is incorrect
C. The function should return true for n <= 1
D. The loop should run until i <= n
Solution
Step 1: Check the if condition inside the loop
The condition uses = which is assignment, not comparison. It should be == or ===.
Step 2: Verify other parts
Loop starting at 2 is correct. Returning false for n <= 1 is correct. Loop running until i < n is correct for prime check.
Final Answer:
The comparison operator in the if condition is incorrect -> Option B
Quick Check:
Use == or === for comparison, not = [OK]
Hint: Use == or === for comparison, not = [OK]
Common Mistakes:
Using = instead of == in conditions
Starting loop from 1 instead of 2
Returning true for numbers <= 1
5. You want to find the least common multiple (LCM) of two numbers using their greatest common divisor (GCD). Which formula correctly relates LCM and GCD for numbers a and b?
hard
A. LCM(a, b) = (a * b) / GCD(a, b)
B. LCM(a, b) = GCD(a, b) / (a * b)
C. LCM(a, b) = a + b - GCD(a, b)
D. LCM(a, b) = a - b + GCD(a, b)
Solution
Step 1: Recall the relationship between LCM and GCD
The formula states that the product of two numbers equals the product of their GCD and LCM.
Step 2: Write the formula for LCM
Rearranging gives LCM(a, b) = (a * b) / GCD(a, b). Options A, C, and D do not match this formula.
Final Answer:
LCM(a, b) = (a * b) / GCD(a, b) -> Option A
Quick Check:
LCM x GCD = a x b [OK]
Hint: Use LCM = (a x b) / GCD to find least common multiple [OK]
Common Mistakes:
Confusing division order in formula
Adding or subtracting instead of multiplying/dividing
Forgetting to use GCD in formula
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.