Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Nested Loops: Index Pairs

Fundamentals: Part 2medium

Course · Section 3: JavaScript Fundamentals – Part 2 · Lecture 49: Looping Backwards and Loops in Loops

Implement `nestedPairs(a, b)` returning every `[i, j]` pair with `i` in `0..a-1` and `j` in `0..b-1`, using nested loops.

Sample tests

Input: nestedPairs(2, 2)
Output: [[0,0],[0,1],[1,0],[1,1]]
Input: nestedPairs(1, 1)
Output: [[0,0]]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Reusing the same loop variable name for both loops.
Approach & explanation (try first)

Nested loops enumerate every combination of outer and inner indices.

Loading...
⌘/Ctrl + Enter

Run your code to see results.