πStar Pattern Printing in JavaScript β Deep Dive

Table of contents
- π Pattern Printing in JavaScript β Deep Dive
- 1οΈβ£ Square of Stars
- 2οΈβ£ Right Triangle of Stars
- 3οΈβ£ Number Triangle (1..j)
- 4οΈβ£ Number Triangle (Row number repeated)
- 5οΈβ£ Inverted Number Triangle
- 6οΈβ£ Inverted Triangle of Stars
- 7οΈβ£ Right-Aligned Triangle (with spaces _)
- 8οΈβ£ Binary Triangle (Row-wise toggle reset)
- 9οΈβ£ Binary Triangle (Global toggle continues across rows)
Letβs break everything down:
π Pattern Printing in JavaScript β Deep Dive
We use nested loops.
Outer loop (
i
) β rowsInner loop (
j
,k
) β columns (how many things to print per row)Conditions (
i < n
,j < n
, etc.) decide when loops stop.
1οΈβ£ Square of Stars
let n = 4;
for (let i = 0; i < n; i++) { // row control
let row = "";
for (let j = 0; j < n; j++) { // column control
row += "*";
}
console.log(row);
}
πΉ Logic
Outer loop β runs
n
times β total rows = 4Inner loop β also runs
n
times β total stars = 4 per row
πΉ Conditions
i < n
ensures loop runs exactly 4 rows.j < n
ensures 4 stars are added per row.
β Output:
****
****
****
****
2οΈβ£ Right Triangle of Stars
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j < i + 1; j++) {
row += "*";
}
console.log(row);
}
πΉ Logic
Outer loop:
i=0..n-1
(row count)Inner loop: runs
i+1
times β row length grows each time
πΉ Conditions
Row 0 β
j < 1
β 1 starRow 1 β
j < 2
β 2 starsRow 2 β
j < 3
β 3 starsRow 3 β
j < 4
β 4 stars
β Output (n=4):
*
**
***
****
3οΈβ£ Number Triangle (1..j)
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j <= i; j++) {
row += (j + 1);
}
console.log(row);
}
πΉ Logic
Outer loop β row count =
n
Inner loop β runs from
0 β i
(inclusive)Prints numbers
1
to(i+1)
πΉ Conditions
j <= i
means row length =i+1
Each row starts from
j=0
so(j+1)
gives 1,2,3,β¦
β Output (n=5):
1
12
123
1234
12345
4οΈβ£ Number Triangle (Row number repeated)
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j <= i; j++) {
row += (i+1);
}
console.log(row);
}
πΉ Logic
Outer loop β row count = n
Inner loop β runs
i+1
timesInstead of
j+1
, we use(i+1)
β repeats row number
πΉ Conditions
Row 0 β
j=0..0
β prints1
Row 1 β
j=0..1
β prints22
Row 2 β
j=0..2
β prints333
β Output:
1
22
333
4444
55555
5οΈβ£ Inverted Number Triangle
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j < n-i; j++) {
row += (j + 1);
}
console.log(row);
}
πΉ Logic
Outer loop β controls row count
Inner loop β length =
n-i
(shrinks each row)
πΉ Conditions
Row 0 β
j < 5
β12345
Row 1 β
j < 4
β1234
Row 2 β
j < 3
β123
Row 3 β
j < 2
β12
Row 4 β
j < 1
β1
β Output:
12345
1234
123
12
1
6οΈβ£ Inverted Triangle of Stars
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j < n-i; j++) {
row += "*";
}
console.log(row);
}
πΉ Logic
- Same as previous but prints stars instead of numbers.
πΉ Conditions
j < n-i
β decreases star count each row.
β Output (n=5):
*****
****
***
**
*
7οΈβ£ Right-Aligned Triangle (with spaces _
)
for (let i = 0; i < n ; i++) {
let row = "";
for (let j = 0; j < n-(i+1); j++) {
row += "_"; // spaces
}
for (let k = 0; k < i + 1; k++) {
row += "*";
}
console.log(row);
}
πΉ Logic
First inner loop β prints spaces
_
Second inner loop β prints stars
*
Spaces decrease, stars increase per row
πΉ Conditions
Row 0 β spaces=4, stars=1
Row 1 β spaces=3, stars=2
Row 2 β spaces=2, stars=3
Row 3 β spaces=1, stars=4
Row 4 β spaces=0, stars=5
β Output:
____*
___**
__***
_****
*****
8οΈβ£ Binary Triangle (Row-wise toggle reset)
for (let i = 0; i < n; i++) {
let row = "";
let toggle = 1;
for (let j = 0; j <= i; j++) {
row += toggle;
toggle = toggle === 1 ? 0 : 1;
}
console.log(row);
}
πΉ Logic
toggle
starts at 1 fresh each rowFlips between
1 β 0 β 1 β 0 β¦
πΉ Conditions
Inner loop
j <= i
β row length growsEach iteration flips toggle
β Output:
1
10
101
1010
10101
9οΈβ£ Binary Triangle (Global toggle continues across rows)
let toggle = 1;
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j <= i; j++) {
row += toggle;
toggle = toggle === 1 ? 0 : 1;
}
console.log(row);
}
πΉ Logic
Global
toggle
β does not reset row by rowPattern continues across all rows
πΉ Conditions
j <= i
β row length increasesToggle value continues from previous row
β Output:
1
01
010
1010
10101
β¨ Key Takeaway (Patterns + Conditions)
j < n
β full row (square)j <= i
β grows with row (triangle)j < n-i
β shrinks with row (inverted)toggle
β switches between 0/1
Subscribe to my newsletter
Read articles from Kamlesh Choudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
