JavaScript Binary Number Triangle Pattern (Starting with 1)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A column-wise alternating binary triangle grows one digit per row while printing j % 2 ascending — every row starts with 1: 1, 10, 101, 1010, …
Remember
Rule: for i = 1 to n
for j = 1 to i
print j % 2
1
10
101
1010
10101 ← n = 5
Twin of Program 15 (1, 01, 101): same j % 2 idea, but the inner loop counts up so even rows start with 1 not 0.
Approach
How to Solve It
Grow row length from 1 to n; on each row, count j up and append j % 2.
Method
Idea
Best for
Nested loops
Outer grows; inner j % 2 ascending
Learning, interviews
Flip parity
1 - (j % 2) starts with 0
Variant practice
prompt input
Same loops; read n at runtime
Interactive practice
Pseudocode
Pseudocode
for i from 1 to n:
line = ""
for j from 1 to i:
line += (j % 2)
print line (with newline)
Cheat sheet
Goal
Pattern
Set size
const n = 5;
Outer loop
for (let i = 1; i <= n; i++)
Inner loop
for (let j = 1; j <= i; j++) line += j % 2;
End row
console.log(line);
Binary digit
j % 2 → 0 or 1
Flip bits
line += 1 - (j % 2);
Printing Numbers vs Starting a New Line
API
Effect
Use for
line += j % 2
Stays on the same row
Each binary digit
console.log(line)
Ends the line
After 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.
Try it
Live Preview
Change n and the column-wise binary triangle updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · digits = 15
1
10
101
1010
10101
Trace
Worked Walkthrough — n = 3
Trace each outer value of i, the ascending j values, and j % 2.
i
j values
j % 2
Printed row
1
1
1
1
2
1, 2
1, 0
10
3
1, 2, 3
1, 0, 1
101
As i grows, more ascending parities are appended — every row still starts with 1.
Code
JavaScript Programs
Three complete programs: fixed n = 5, a flipped-parity variant, and prompt input. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
1. Same loops. Outer and inner bounds match Example 1.
2. Flip the bit.1 - (j % 2) turns 1 into 0 and 0 into 1.
3. Mirrored start. Row 1 becomes 0; every row now starts with 0.
Example 3 — prompt Input
Read n at runtime with prompt and parseInt.
JavaScript
const n = parseInt(prompt("Enter the number of rows:"), 10);
if (!Number.isFinite(n) || n < 1) {
console.log("Please enter a positive integer.");
} else {
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = 1; j <= i; j++) {
line += j % 2;
}
console.log(line);
}
}
Vs Program 15: ascending j keeps every row starting with 1.
Write vs log:line += j % 2 builds; console.log(line) breaks.
Complexity:O(n²) for n rows.
One line: grow the row from 1 to n, and append each ascending index modulo 2.
Frequently Asked Questions
Modulo 2 returns the remainder after dividing by 2. Any integer is either even (remainder 0) or odd (remainder 1).
On row 2, the inner loop prints j = 1 then j = 2. That becomes 1 % 2 = 1 then 2 % 2 = 0, so the row is 10.
line += j % 2 stays on the same row while building binary digits. console.log(line) prints the completed row and adds a newline.
Program 16 counts the inner loop up (j = 1..i) producing 1, 10, 101, 1010. Program 15 counts down (j = i..1) producing 1, 01, 101, 0101.
Yes. Use 1 - (j % 2) instead of j % 2 to flip every digit — first row becomes 0 instead of 1.
O(n²) where n is the number of rows. Total digit appends equal 1+2+…+n = n(n+1)/2.
Use parseInt(prompt(...), 10) and check Number.isFinite(n) && n >= 1 so bad input does not produce NaN.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Each row prints alternating 0 and 1 using j % 2. The inner loop counts up with for (let j = 1; j <= i; j++), so every row starts with 1 — still O(n²) total appends.