The Lone Pair
MediumAcc. 87.4%
+30 XP 12
Precise Searching
In a large search space, you often want to stop the moment you find your target. When using nested loops, a break statement will exit the innermost loop, allowing the outer loop to continue to its next cycle.
The Assignment
Your function receives parameters max and targetSum.
- Write an outer
forloop (i) from 1 tomax. - Write an inner
forloop (j) from 1 tomax. - Inside the inner loop, check if the sum
i + jis exactlytargetSum. - If the sum is matched:
- Print the matching pair: (i, j)
- Use
breakto exit the inner loop immediately.
- After the inner loop finishes (for every iteration of the outer loop), print a marker: "-" (a single dash).
01EXAMPLE 1
Input
max = 5Output
-
(2, 5)
-
(3, 4)
-
(4, 3)
-
(5, 2)
-Explanation: Stops inner loop at the first pair for each row.
Constraints
- Use nested for loops.
- Use a break statement inside the condition.
- Log messages must match exactly.
LoopsControl FlowFundamentals
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
max5
targetSum7
Expected Output
- (2, 5) - (3, 4) - (4, 3) - (5, 2) -
Click RUN to test your solution