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.

  1. Declare a let variable divisor and initialize it to 2.
  2. Write a while loop that runs as long as divisor is less than target.
  3. Inside the loop:
    • Check if target is divisible by divisor (target % divisor === 0).
    • If true: Print "Composite number found, stopping." and use break.
    • Otherwise: Increment divisor by 1.
  4. After the loop:
    • If divisor is equal to target, print: "Prime number."

01EXAMPLE 1

Inputtarget = 13
OutputPrime number.

Explanation: No divisors found between 2 and 12.

02EXAMPLE 2

Inputtarget = 4
OutputComposite number found, stopping.

Explanation: 2 divides 4, so it breaks early.

Constraints

  • Use a break statement.
  • Log messages must match exactly.
LoopsFundamentalsLogic
JavaScript
Loading...
4 Hidden

Input Arguments

target13

Expected Output

Prime number.

Click RUN to test your solution