The Prime Breaker
EasyAcc. 93.1%
+20 XP 8
Early Exit
Sometimes you don't need to finish the whole loop. If you find what you're looking for (like a number that divides perfectly), the break keyword allows you to shatter the loop and leave immediately.
The Assignment
Your function receives a parameter named target.
- Declare a let variable
divisorand initialize it to 2. - Write a
whileloop that runs as long asdivisoris less thantarget. - Inside the loop:
- Check if
targetis divisible bydivisor(target % divisor === 0). - If true: Print "Composite number found, stopping." and use
break. - Otherwise: Increment
divisorby 1.
- Check if
- After the loop:
- If
divisoris equal totarget, print: "Prime number."
- If
01EXAMPLE 1
Input
target = 13Output
Prime number.Explanation: No divisors found between 2 and 12.
02EXAMPLE 2
Input
target = 4Output
Composite number found, stopping.Explanation: 2 divides 4, so it breaks early.
Constraints
- Use a break statement.
- Log messages must match exactly.
LoopsFundamentalsLogic
JavaScriptSystem handles I/O — write your function only
Loading...
4 Hidden
Input Arguments
target13
Expected Output
Prime number.
Click RUN to test your solution