Shape Rule
Diagonal star
Each row prints descending digits with one * where i === j — the star moves left each row.

The descending number pattern with diagonal asterisk prints 5432*, 543*1, 54*21, 5*321, *4321 — a natural step after the bidirectional triangle in Program 25. This tutorial covers descending digits, the i === j condition, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
Diagonal star
Each row prints descending digits with one * where i === j — the star moves left each row.
i = 1..n
for (let i = 1; i <= n; i++) walks each row top to bottom.
j = n..1
for (let j = n; j >= 1; j--) prints digits 5, 4, 3, 2, 1 per row.
i === j
When i === j, print *; otherwise print j.
3–9 size
Pick a size n and draw the diagonal asterisk pattern instantly in the browser.
Complexity
Each row prints n characters; total work scales as n².
A descending number pattern with diagonal asterisk prints digits from n down to 1 on each row, replacing one position with * where i === j. With n = 5, the output is 5432*, 543*1, 54*21, 5*321, *4321.
In JavaScript you use an outer loop for rows, a descending inner loop for columns, and an if i === j to swap a digit for a star.
It combines row/column indexing with a conditional swap — a step up from Program 25’s digit mapping.
j = n..1 — digits decrease left to right.
i === j marks the star position.
As i grows, the star shifts left each row.
Follow Program 25; continue to Program 27 (palindrome triangle) next.
In short: for each i, scan j from n down to 1 — append * when i === j, else append j, then console.log(line).
Given a positive integer n (e.g. 5), print n rows of descending digits with one diagonal * per row where i === j.
# n = 5 (conceptual shape)
# 5432*
# 543*1
# 54*21
# 5*321
# *4321 | Item | Type | Description |
|---|---|---|
n | int | Pattern size — outer loop runs from 1 to n. |
i | int | Outer loop — current row number; also the diagonal star column. |
j | int | Inner loop — descending column digit from n down to 1. |
| Output | char | * when i === j; otherwise the digit j. |
for i from 1 to n:
line = ""
for j from n down to 1:
if i === j:
line += "*"
else:
line += j
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| if i === j | 5432*, 543*1, … | Learning and interviews |
| Custom symbol | Replace * with # or any char | Visual variants |
| User-input n | const n = parseInt(prompt(...), 10) | Flexible console programs |
| Goal | Pattern |
|---|---|
| Walk rows | for (let i = 1; i <= n; i++) |
| Descending columns | for (let j = n; j >= 1; j--) |
| Diagonal star | if (i === j) { line += "*"; } |
| Otherwise digit | else { line += j; } |
| End the row | console.log(line) |
| User input | const n = parseInt(prompt(...), 10) |
Same diagonal asterisk pattern — different ways to control size and the replacement character.
i = 1..nRow index doubles as star column
j = n..1Descending digits per row
i === jSwap digit for star on diagonal
if/elseOne inner loop handles star vs digit
Reach for this pattern when teaching row/column indexing, conditional character substitution, and diagonal effects in nested loops.
Natural follow-up after Program 25 — introduces i === j diagonal substitution.
Outer/inner bound practice with an immediate visual check.
Combine loops with prompt() for a flexible row count.
Compare Program 25 (bidirectional triangle) and Program 27 (palindrome triangle) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a size between 3 and 9 and draw the descending diagonal asterisk pattern in the browser.
Three complete JavaScript programs — fixed size, custom symbol, and user input. Click View Output to reveal sample console results, or Try it Yourself to run the code live.
Print five rows of the diagonal asterisk pattern with i === j.
n = 5Hard-coded size — ideal for first demos and screenshots.
const n = 5;
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = n; j >= 1; j--) {
if (i === j) {
line += "*";
} else {
line += j;
}
}
console.log(line);
} When i = 1, the star lands at j = 1 (rightmost) — output 5432*. When i = 5, the star is at the leftmost position — output *4321. Each row always prints n characters.
Replace the diagonal asterisk with another character like #.
#Keep n = 5 but use hash instead of asterisk on the diagonal.
const n = 5;
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = n; j >= 1; j--) {
if (i === j) {
line += "#";
} else {
line += j;
}
}
console.log(line);
} Only the replacement character changes — "#" instead of "*". Loop bounds and the i === j condition stay the same as Example 1.
Read the pattern size with prompt() instead of hard-coding 5.
Read n with prompt() and parseInt(); both loops use n as the bound.
const nInput = prompt("Enter size:");
const n = parseInt(nInput, 10);
for (let i = 1; i <= n; i++) {
let line = "";
for (let j = n; j >= 1; j--) {
if (i === j) {
line += "*";
} else {
line += j;
}
}
console.log(line);
} Same i === j core as Example 1; only the source of n changes. The diagonal star scales with the user’s input. Non-numeric input yields NaN with bare parseInt(prompt(), 10) — validate with Number.isFinite for safer labs.
console.log is built in; use prompt() when reading input. Set loop variables i, j and n = 5.
for (let i = 1; i <= n; i++) — row index also marks the star column.
for (let j = n; j >= 1; j--) — prints digits n..1 per row.
if (i === j) appends *; else line += j.
console.log(line) ends the row after the inner loop.
Star moves left each row — O(n²) time, O(1) extra memory.
n = 5Trace each outer-loop value of i, where i === j, and the full row output.
i | Star at j | Digits printed | Row output |
|---|---|---|---|
1 | j = 1 | 5, 4, 3, 2, * | 5432* |
2 | j = 2 | 5, 4, 3, *, 1 | 543*1 |
3 | j = 3 | 5, 4, *, 2, 1 | 54*21 |
4 | j = 4 | 5, *, 3, 2, 1 | 5*321 |
5 | j = 5 | *, 4, 3, 2, 1 | *4321 |
The star position moves left as i increases — each row still prints exactly n characters.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j = n..1 to j = 1..n and watch digit order flip.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: use i + j == n + 1 for the anti-diagonal star.
Practice line += vs console.log(line) without complex math.
Example: put console.log(line) inside the inner loop by mistake.
Swap digits for letters or add spaces once the loop works.
Example: replace * with # or a space character.
Triangular totals make O(n²) concrete for beginners.
Example: count printed chars for n = 5 → 5 × 5 = 25.
Pair the pattern with Number.isFinite and positive-n checks.
Example: reject n <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner JavaScript courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i and j on paper for n = 3 before coding — the star column is where they meet.
Small habits that keep number-pattern code clean.
Do not print i in the else branch — use j for the descending digit.
prompt()Validate parseInt(prompt(), 10) with Number.isFinite so bad input does not produce NaN.
console.log(line) OutsideOnly call console.log(line) after the inner loop finishes the row.
Mark where i === j on each row before coding.
Trace n = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put console.log(line) inside the inner loop.
Mistakes that commonly break diagonal asterisk patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use line += "*" or line += j; console.log(line) only after the inner loop.
Flipping the condition prints stars everywhere except the diagonal.
→ Print * when i === j, not when they differ.
Using j = 1..n reverses the digit order on each row.
→ Use for (let j = n; j >= 1; j--) for descending digits.
Writing line += i in the else branch repeats the row number, not the column digit.
→ Print j in the else branch — it holds the descending column value.
Letters or empty input yield NaN from bare parseInt(prompt(), 10).
→ Validate with Number.isFinite and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just * — one star, one row.
Outer loop never runs — print nothing or show a message.
n < 0Treat as invalid; re-prompt instead of silent empty output.
Two rows: 2* and *1.
parseInt(prompt(), 10) yields NaN — validate with Number.isFinite first.
Output grows as n² characters — fine for labs, noisy for huge n.
Try these variations to lock in the pattern.
i + j == n + 1 instead of i === j* when i === j or i + j == n + 1i === j, print *; otherwise print descending digit j.line += stays on the line; console.log(line) advances — mix them carefully.n > 0 for interactive programs; n = 1 prints a single *.#, X, or a space.Quick Takeaway: outer loop i = 1..n, descending inner loop j = n..1, append * when i === j else j, then console.log(line) after each row.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1 & 3) | O(n²) | O(1) |
| Custom symbol (Example 2) | O(n²) | O(1) |
The descending number pattern with diagonal asterisk is a compact lesson in row/column indexing: print descending j values and swap one position with * when i === j. Master the fixed-n version, then try a custom symbol and user input.
Practice the three examples above, then continue to Program 27 for the palindrome number triangle.
Print j in the else branch, not i — validate n when reading from the console.
for (let i = 1; i <= n; i++) in the outer loopfor (let j = n; j >= 1; j--)* when i === j, else print jparseInt(prompt(), 10) with Number.isFinite before using nconsole.log(line) inside the inner loopi instead of j in the else branchi != jj unless you want reversed digitsn = 1 edge casePrint the pattern the beginner-friendly way.
i === j → *
DefinitionDescending
Code* or j
CodeLeft each row
ShapeO(n²) time
AnalysisThis pattern prints descending numbers from n to 1 on each row. When the row index equals the current column value (i === j), it appends * instead of the number, creating a diagonal asterisk that moves left each row.
Move on to the palindrome number triangle in the JavaScript number-pattern series.
12 people found this page helpful