JavaScript Binary Rows Pattern (Alternating 1 and 0)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An alternating 1/0 triangle prints a shrinking row of the same digit each time — odd rows are all 1s, even rows are all 0s — chosen with i % 2.

Remember
Rule: for i = 1..rows
      print (i % 2) repeated (rows - i + 1) times

11111
0000
111
00
1           ← rows = 5

After Program 39’s rotating digits, this pattern focuses on parity and a shrinking inner bound.

How to Solve It

Walk i from 1 to rows; repeat i % 2 from j = i to rows.

MethodIdeaBest for
Parity + shrinkDigit = i % 2; inner j = i..rowsLearning, interviews
prompt rowsSame loops; read rows at runtimeInteractive practice
Spaced digitsSame bounds; line += (i % 2) + " "Readable console output

Pseudocode

Pseudocode
for i from 1 to rows:
    line = ""
    for j from i to rows:
        line += (i mod 2)
    print line

Cheat sheet

GoalPattern
Set rowsconst rows = 5;
Outer loopfor (let i = 1; i <= rows; i++)
Inner loopfor (let j = i; j <= rows; j++)
Row digitline += i % 2;
End rowconsole.log(line);
Chars per rowrows - i + 1
Flip parityline += 1 - (i % 2);

Printing Numbers vs Starting a New Line

APIEffectUse for
line += i % 2Stays on the same rowEach digit
console.log(line)Ends the lineAfter the inner loop

Build the row with +=, then break once with console.log. Putting console.log inside the inner loop prints one digit per line.

Live Preview

Change the row count and the alternating triangle updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · digits = 15
11111
0000
111
00
1

Worked Walkthrough — rows = 3

Trace each outer i, the parity digit, how many times it repeats, and the printed row.

ii % 2RepeatsPrinted row
113111
20200
3111

Repeats = rows - i + 1. Total digits = 6 = 3×4/2.

JavaScript Programs

Three complete programs: fixed rows = 5, prompt rows, and spaced digits. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed rows = 5

Hard-coded height — shrink the inner loop and append i % 2 each time.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = i; j <= rows; j++) {
    line += i % 2;
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop. i is both the row index and the parity source via i % 2.

2. Inner loop. j runs from i to rows, so the row shortens by one each time.

3. Digit. Odd i appends 1; even i appends 0. Then console.log(line).

Example 2 — prompt Rows

Read rows at runtime with prompt and parseInt.

JavaScript
const rows = parseInt(prompt("Enter rows:"), 10);

if (!Number.isFinite(rows) || rows < 1) {
  console.log("Please enter a positive integer.");
} else {
  for (let i = 1; i <= rows; i++) {
    let line = "";
    for (let j = i; j <= rows; j++) {
      line += i % 2;
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and validate. Parse the answer; reject non-positive or non-numeric input.

2. Same parity rule. Odd rows still print 1s; even rows print 0s.

3. Only height changes. Inner and outer bounds still use the chosen rows.

Example 3 — Spaced Digits

Same loop bounds; append a space after each digit for easier reading.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = i; j <= rows; j++) {
    line += (i % 2) + " ";
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Same structure. Outer and inner bounds match Example 1.

2. Only the append changes. line += (i % 2) + " " adds a trailing space after each digit.

3. Shape unchanged. Still odd = 1, even = 0, shrinking each row.

Edge Cases & Pitfalls

Check these before calling the solution done.

0-based i

Flipped parity

Starting i at 0 makes the first row all 0s. Keep i = 1..rows to start with 1s.

fixed inner

Full rectangle

If the inner loop always runs 1..rows, every row has the same length. Start at j = i.

log inside

Vertical digits

If console.log is inside the inner loop, each digit lands on its own line.

j % 2

Checkerboard mix

Using j % 2 instead of i % 2 alternates inside the row, not across rows.

rows = 1

Single digit

Output is just 1 — one outer iteration, one append.

NaN input

Validate parseInt

Letters or empty prompt yield NaN — check Number.isFinite(rows) && rows >= 1.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(n) for the current line string

Digits per pattern = n + (n-1) + … + 1 = n(n+1)/2 → O(n²).

Key Takeaways

  • Rule: append i % 2 for j = i..rows on each row.
  • Parity: odd rows → 1; even rows → 0.
  • Write vs log: line += i % 2 builds; console.log(line) breaks.
  • Complexity: total digits = n(n+1)/2 → O(n²).

One line: shrink each row while flipping between 1 and 0 with i % 2.

Frequently Asked Questions

It appends i % 2 on each row. When i is odd the row is all 1s; when i is even the row is all 0s.
The inner loop runs for (let j = i; j <= rows; j++), appending rows - i + 1 characters per row — decreasing from rows down to 1.
Append 1 - (i % 2) instead of i % 2, or swap the if/else logic.
Program 39 rotates digits 1..n per row. Program 40 appends only 1 or 0 per row based on parity, with shrinking row length.
Replace 5 with rows in the outer loop bound — see Example 2.
Use line += (i % 2) + " " instead of line += i % 2 — see Example 3.
O(n²) for n rows because total appends are 1 + 2 + … + n = n(n+1)/2.
Use parseInt with Number.isFinite so bad input does not produce NaN.
Only one row prints — a single 1.

Did you know?

Odd rows append 1, even rows append 0 — chosen with i % 2. Row i appends rows - i + 1 characters; total appends = n(n+1)/2.

Next: Square Numbers Pyramid

Continue with the next pattern in the JavaScript number-pattern series.

Program 41 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful