X Rule
Two diagonals
Each row prints one letter at left and right columns; all other columns are spaces.

Each row places one letter on two diagonals forming an X shape: row 0 prints A at both ends, each next row steps inward with B, C, D, until a single E lands in the center for five rows. A column loop with left = r and right = width - 1 - r teaches 2D grid thinking. Compare with star X pattern (0 and *). Includes a live preview, worked JavaScript examples, edge cases, and complexity.
Two diagonals
Each row prints one letter at left and right columns; all other columns are spaces.
col = r
left = r moves one column right each row - top-left to center.
col = width - 1 - r
right = width - 1 - r moves one column left each row - top-right to center.
2*rows - 1
width = 2 * rows - 1 gives equal space on both sides of the center column.
1–26 rows
Pick a row count and draw the alphabet X pattern in the browser instantly.
Complexity
n rows × 2n - 1 columns per row ≈ O(n²); extra memory stays O(1).
An alphabet X pattern places letters on two diagonals inside a grid of width 2*rows - 1. Row 0 prints A at both ends; each row steps inward until the diagonals meet at one center letter.
In JavaScript you solve it with an outer loop over row index r, an inner column loop, and charCodeAt(0)/String.fromCharCode() - or build each row in an array and console.log(row.join("")) for clarity.
It teaches 2D grid coordinates - mapping row and column indices to print positions - the same skill used in matrices, game boards, and ASCII art. When left == right, both diagonals meet and only one character prints.
width = 2 * rows - 1 - for rows=5 the grid is 9 columns wide.
left = r - column index grows one step right each row.
right = width - 1 - r - column index shrinks one step left each row.
When left == right, only one letter prints - not two copies.
In short: set width = 2 * rows - 1 and base = "A".charCodeAt(0), loop r from 0 to rows - 1, compute left = r and right = width - 1 - r, append the row letter at those columns and spaces elsewhere, then console.log(line) for the newline.
Given a positive integer rows, print an X-shaped grid of rows lines, each 2*rows - 1 characters wide. Row r prints String.fromCharCode("A".charCodeAt(0) + r) at columns left = r and right = width - 1 - r; all other positions are spaces.
// First 5 rows (width = 9)
// A A
// B B
// C C
// D D
// E | Item | Type | Description |
|---|---|---|
rows | int | Number of rows (also the row letter runs A through the rows-th letter). Clamp to 1–26 for A–Z demos. |
| Printed output | text | X-shaped grid: letters on two diagonals, spaces elsewhere - width 2*rows - 1 per line. |
width = 2 * rows - 1
base = "A".charCodeAt(0)
for r from 0 to rows-1:
ch = String.fromCharCode(base + r)
left = r
right = width - 1 - r
for c from 0 to width-1:
append ch if c==left or c==right else space
console.log(line) | Approach | Idea | Best for |
|---|---|---|
| Column loop | for (let c = 0; c < width; c++) append letter or space | Learning 2D grid coordinates and diagonals |
| Join array | Build row in an array, console.log(row.join("")) | Clearer debugging and row inspection |
| Star X contrast | See Program 45 (0 and * X) | Same diagonal logic with symbols instead of letters |
| Goal | Pattern |
|---|---|
| Grid width | width = 2 * rows - 1 |
| Outer loop (row index) | for (let r = 0; r < rows; r++) |
| Row letter | ch = String.fromCharCode(base + r) |
| Diagonal columns | left = r; right = width - 1 - r |
| Column loop | for (let c = 0; c < width; c++) line += (c === left || c === right) ? ch : " " |
| End the row | console.log(line) |
| Array join variant | row.push(c === left || c === right ? ch : " "); console.log(row.join("")) |
Three ways to think about the same X grid - pick based on what you are learning.
left = r
col moves rightTop-left to center - column index equals row index.
right = width-1-r
col moves leftTop-right to center - column index shrinks as rows grow.
row.push(...)
row.join("")Collect characters in an array, then log one string - easier to inspect each row while debugging.
Reach for diagonal column loops when you need letters (or symbols) on two crossing lines inside a fixed-width grid.
Program 30 mixes prefix and suffix on one line. This pattern uses a 2D grid with diagonal columns.
Practice mapping (r, c) pairs to print positions before tackling matrices.
Same left/right column rule appears in star X patterns and hollow diamond shapes.
Next pattern builds a centered palindrome pyramid - another symmetric shape.
This is a console teaching pattern - not how you build modern app screens.
Key benefit: one program that proves you can map row/column indices to diagonal positions - a pattern used in matrices, game boards, and ASCII art far beyond alphabet demos.
Choose a row count between 1 and 26 and draw the alphabet X pattern in the browser.
Three complete JavaScript programs - fixed five rows with diagonal column loops, prompt input with ternary form, and an array-join variant for clarity. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print five rows of the alphabet X pattern with left/right diagonal column loops.
rows = 5Hard-coded height - ideal for first demos and screenshots.
let rows = 5;
rows = Math.max(1, Math.min(rows, 26));
const width = 2 * rows - 1;
const base = "A".charCodeAt(0);
for (let r = 0; r < rows; r++) { // 0..rows-1
const ch = String.fromCharCode(base + r);
const left = r;
const right = width - 1 - r;
let line = "";
for (let c = 0; c < width; c++) {
if (c === left || c === right) {
line += ch;
} else {
line += " ";
}
}
console.log(line);
} The outer loop walks row index r from 0 to 4. For each row, ch = String.fromCharCode(base + r) selects the letter and left = r, right = width - 1 - r mark the two diagonal columns. The inner loop scans every column: append the letter when c === left or c === right, otherwise append a space. On the last row left === right === 4, so only one E appears in the center.
Let the user choose the height at runtime.
Read rows and clamp to 1–26. Validate parseInt(prompt(), 10) with Number.isFinite in real apps.
let rows = parseInt(prompt("Enter number of rows (max 26):"), 10);
if (!Number.isFinite(rows)) {
console.log("Please enter a whole number.");
} else {
rows = Math.max(1, Math.min(rows, 26));
const width = 2 * rows - 1;
const base = "A".charCodeAt(0);
for (let r = 0; r < rows; r++) {
const ch = String.fromCharCode(base + r);
const left = r;
const right = width - 1 - r;
let line = "";
for (let c = 0; c < width; c++) {
line += (c === left || c === right) ? ch : " ";
}
console.log(line);
}
} Same diagonal grid core as Example 1; only the row count comes from prompt. Three rows use width 5 with letters A–C on the diagonals - row 2 prints B at columns 1 and 3.
Build each row in an array, then log with row.join("").
row.join("") VariantCollect characters in an array for clearer row inspection - same logic, easier debugging.
let rows = 5;
rows = Math.max(1, Math.min(rows, 26));
const width = 2 * rows - 1;
const base = "A".charCodeAt(0);
for (let r = 0; r < rows; r++) {
const ch = String.fromCharCode(base + r);
const left = r;
const right = width - 1 - r;
const row = [];
for (let c = 0; c < width; c++) {
row.push((c === left || c === right) ? ch : " ");
}
console.log(row.join(""));
} The column loop pushes each character to row instead of appending to a string immediately. row.join("") builds the full line - identical output to Examples 1 and 2, but you can inspect row before logging during debugging.
Clamp rows, then set width = 2 * rows - 1 and base = "A".charCodeAt(0) for the grid and alphabet.
for (let r = 0; r < rows; r++) walks each row from 0 to rows - 1, computing left, right, and ch.
for (let c = 0; c < width; c++) appends the letter at diagonal columns and spaces elsewhere with line += ....
console.log(line) ends the row after the column loop finishes; the outer loop advances r to the next row.
Total characters: n × (2n - 1) ≈ O(n²) — O(n²) time, O(1) extra memory (loop version).
rows = 5Trace each outer-loop value of r and see how left, right, and the row letter produce each printed line.
r | Letter | left | right | Row output |
|---|---|---|---|---|
| 0 | A | 0 | 8 | A A |
| 1 | B | 1 | 7 | B B |
| 2 | C | 2 | 6 | C C |
| 3 | D | 3 | 5 | D D |
| 4 | E | 4 | 4 | E (left==right) |
Total character prints: 5 × 9 = 45 = rows × width for rows = 5.
Where diagonal grid patterns show up beyond the homework prompt.
Program 30 mixes prefix and suffix on one line. This pattern uses a 2D grid with diagonal columns.
Example: compare fixed-width rows vs spaced X grid side by side.
Reinforce mapping (r, c) pairs to print positions before tackling matrices.
Example: trace left and right for row 2 (r=1) on paper before coding.
Same diagonal positions with * instead of letters - see Program 45.
Example: swap ch for * and keep the column loop.
Swap letters for digits 1..n at the same diagonal columns.
Example: rows=3 prints 1 at corners and 3 in the center.
Rectangular totals make O(n²) concrete for beginners.
Example: 5 rows → 45 characters printed (5 × 9).
Classic nested-loop question that tests diagonal column indices and center merge.
Example: explain why only one E prints when rows=5 without running code.
Pro Tip: say “letter at left and right columns, space everywhere else” before coding - that story prevents wrong width or missing spaces.
Why this pattern earns a spot after the mixed alphabet rows from Program 30.
Row and column loops map directly to matrix coordinates - a core programming skill.
The X is visually symmetric - left and right diagonals mirror each other.
Line-build version for learning; array-join version for clearer debugging.
Streaming output needs no storage beyond loop counters (join variant uses O(width) per row).
Pro Tip: when left == right on the last row, the or condition still prints exactly once per column - no special case needed.
Small habits that keep alphabet X pattern code clean.
Use left = r and right = width - 1 - r - keep r and c for loop variables.
parseInt(prompt(), 10) in try/exceptAvoid crashes when the user types letters instead of a number.
rows = Math.max(1, Math.min(rows, 26)) keeps demos inside A–Z.
width = 2 * rows - 1 before the row loop - do not recompute inside unless rows changes.
Trace A at corners, B, center C on paper before coding larger demos.
Pro Tip: if the shape looks like a solid block, you likely forgot spaces - only diagonal columns should print letters.
Mistakes that commonly break alphabet X patterns.
Using width = rows or width = 2 * rows misaligns the diagonals - the X looks skewed or truncated.
→ Use width = 2 * rows - 1 so the top row has equal space on both sides.
Some beginners add a separate branch when left == right and accidentally print the letter twice.
→ The condition c == left or c == right already prints once per column - no extra branch needed.
Magic numbers like String.fromCharCode(65 + r) work but break readability and lowercase variants.
→ Use base = "A".charCodeAt(0) and String.fromCharCode(base + r) instead of raw ASCII values.
Non-numeric input yields NaN with bare parseInt(prompt(), 10).
→ Validate with Number.isFinite and clamp the range.
Printing only letters without spaces collapses the X into a solid diagonal block.
→ The inner loop must append a space character for every non-diagonal column.
Check these inputs before calling the solution done.
Output is just A on one line - left == right == 0, so one letter at column 0.
Treat as invalid; re-prompt instead of silent empty output.
26 rows with width 51 - last row prints Z once at the center column.
Clamp to 26 or define a wrap/error policy before printing.
Use Number.isFinite before clamping rows.
Same loops work with base = "a".charCodeAt(0) and lowercase output.
Try these variations to lock in the pattern.
row.push and row.join("")* and 0n rows is n × (2n - 1) - each row scans the full width.for (let c = 0; c < width; c++) with left = r and right = width - 1 - r.row.join("") after building in an array is equivalent to the line-build version - use whichever fits your lesson.Z once at the center when diagonals meet.Quick Takeaway: outer loop sets r, compute left and right, print the row letter at diagonal columns and spaces elsewhere, then break the line - that is the whole alphabet X pattern.
| Program | Time | Extra space |
|---|---|---|
| Column loop (Examples 1–2) | O(rows²) | O(1) |
| Join variant (Example 3) | O(rows²) | O(width) for the row list per line |
The alphabet X pattern teaches 2D grid thinking - mapping row and column indices to diagonal print positions. Master the line-build version, then try the array-join variant for clearer debugging.
Practice the three examples above, then continue to Program 32 in the alphabet pattern series.
Set width, left, and right each row, run the column loop, clamp rows to 26, and compare with star X pattern (Program 45) using the same diagonal logic.
base = "A".charCodeAt(0), width = 2 * rows - 1left = r and right = width - 1 - r each rowline += ch or a ternary in the column loop; console.log(line) afterwidth formula - diagonals will not align"A".charCodeAt(0)left == right - one column already prints onceconsole.log inside the column loopPrint the X grid the beginner-friendly way.
Two diagonals
Definitionleft = r
Codewidth - 1 - r
Coderow.join("")
AltO(n²) time
AnalysisRow r prints letter String.fromCharCode('A'.charCodeAt(0) + r) at columns left = r and right = width - 1 - r in a grid of width 2*rows - 1. On the last row both diagonals meet, so only one E appears in the center.
Next up: the centered alphabet palindrome pyramid - build on symmetric shape ideas from this tutorial.
12 people found this page helpful