Shape Rule
Shifting start, fixed end
Row 0 prints ABCDE, row 1 prints BCDE, row 2 prints CDE, down to a single E on the last row.

The shifting-start alphabet pattern keeps a fixed end letter on every row while the start letter moves right each line. This tutorial covers the shape rule, fixed end formula, a live preview, algorithm steps, worked JavaScript examples, edge cases, and complexity.
Shifting start, fixed end
Row 0 prints ABCDE, row 1 prints BCDE, row 2 prints CDE, down to a single E on the last row.
Row index
for (let i = 0; i < rows; i++) picks the starting letter for each row (0-based index).
start..end letters
for (let code = start; code <= end; code++) appends from the row start through the fixed end letter.
Same line / next line
Letters use line += String.fromCharCode(code); end each row with console.log(line).
1–26 rows
Pick a row count and draw the shifting-start pattern instantly in the browser.
Complexity
Total letters = n(n+1)/2; extra memory stays O(1).
A shifting-start alphabet pattern keeps the same end letter on every row while the first letter moves one step right each line. With five rows the console shows ABCDE, BCDE, CDE, DE, E — the complement of Program 5’s fixed-start pattern.
In JavaScript you solve it with two nested for loops: compute a fixed end = "A".charCodeAt(0) + rows - 1, set start = "A".charCodeAt(0) + i per row, append letters from start through end, then call console.log(line) for the next line.
It teaches per-row start bounds with a shared end — the mirror of Program 5. Once end = base + rows - 1 and code <= end click, left-trim and pyramid variants follow naturally.
end = "A".charCodeAt(0) + rows - 1 — for five rows, every row ends at E.
Row i starts at String.fromCharCode("A".charCodeAt(0) + i) — A, then B, then C, and so on.
line += String.fromCharCode(code) in the inner loop; console.log(line) after.
Program 5 trims from the end; this pattern trims from the start — compare both side by side.
In short: for each row i from 0 to rows - 1, append letters from start = "A".charCodeAt(0) + i through end = "A".charCodeAt(0) + rows - 1 with line += String.fromCharCode(code), then call console.log(line).
Given a positive integer rows, print a left-aligned shifting-start alphabet pattern: each row starts one letter later, but every row ends at the same fixed letter (E when rows = 5).
# First 5 rows (conceptual shape)
# ABCDE
# BCDE
# CDE
# DE
# E | Item | Type | Description |
|---|---|---|
rows | int | Number of pattern lines to print (typically ≥ 1). |
end | int (code) | Fixed last letter: "A".charCodeAt(0) + rows - 1. |
| Printed output | text | Left-aligned rows; row i prints from String.fromCharCode("A".charCodeAt(0) + i) through the fixed end letter. |
end = "A".charCodeAt(0) + rows - 1
for i from 0 to rows - 1:
start = "A".charCodeAt(0) + i
for code from start to end:
append letter to line
log line | Approach | Idea | Best for |
|---|---|---|
| Nested loops | Shifting start + fixed end | Learning and interviews |
| Fixed end formula | end = "A".charCodeAt(0) + rows - 1 | This pattern — shared last letter |
letters.slice(i, rows) | Slice from row start through fixed width | Shorter production-style demos |
| Goal | Pattern |
|---|---|
| Fixed end letter | end = "A".charCodeAt(0) + rows - 1 |
| Walk each row | for (let i = 0; i < rows; i++) |
| Row start letter | start = "A".charCodeAt(0) + i |
| Print start..end letters | for (let code = start; code <= end; code++) { line += String.fromCharCode(code); } |
| End the row | console.log(line) |
| One-line row shortcut | console.log(letters.slice(i, rows)) |
| Fixed-start variant | See Program 5 — start stays at A |
Same ABCDE-to-E shape — three ways to think about row bounds.
end = base + rows - 1Every row ends at the same letter — E when rows = 5
start = base + iRow i drops leading letters — A, then B, then C, and so on
whole rowBuilds each row at once — skip the inner loop
loops firstMaster nested loops before the string shortcut
Reach for this pattern when teaching fixed end bounds with a shifting start — the mirror of Program 5’s fixed-start shape.
Natural follow-up after Program 5 — same row count, opposite trim direction.
Practice code <= end with an immediate visual check.
Combine loops with prompt() for a flexible row count.
Leads to reverse patterns, pyramids, and hollow shapes in the series.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in fixed-end bounds, shifting starts, output sequencing, and O(n²) thinking — the mirror image of Program 5.
Choose a row count between 1 and 26 and draw the shifting-start alphabet pattern in the browser.
Three complete JavaScript programs — fixed row count, prompt input, and a letters.slice(i, rows) shortcut. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.
Print five rows with classic nested loops — shifting start, fixed end.
rows = 5Hard-coded height — ideal for first demos and screenshots.
const rows = 5;
const base = "A".charCodeAt(0);
const end = base + rows - 1; // 'E' when rows = 5
for (let i = 0; i < rows; i++) {
const start = base + i;
let line = "";
for (let code = start; code <= end; code++) {
line += String.fromCharCode(code);
}
console.log(line);
} When i = 0, start is A and the inner loop prints ABCDE. When i = 2, start is C and the row is CDE. When i = 4, start and end are both E, so the last row is a single E. console.log(line) after the inner loop starts the next row.
Let the user choose the height at runtime.
Read the row count with prompt() and convert with parseInt() (validate with Number.isFinite in real apps).
const rowsInput = prompt("Enter the number of rows (max 26):");
let rows = parseInt(rowsInput, 10);
rows = Math.max(1, Math.min(rows, 26));
const base = "A".charCodeAt(0);
const end = base + rows - 1;
for (let i = 0; i < rows; i++) {
const start = base + i;
let line = "";
for (let code = start; code <= end; code++) {
line += String.fromCharCode(code);
}
console.log(line);
} Same charCodeAt/fromCharCode core as Example 1; only the source of rows changes. The clamp keeps letter codes within A–Z. Non-numeric input returns NaN with bare parseInt(prompt()) — validate with Number.isFinite for safer labs.
Same shape without an explicit inner letter loop.
letters.slice(i, rows)Slice A–Z from index i through rows for each row.
const rows = 5;
const clampedRows = Math.max(1, Math.min(rows, 26));
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 0; i < clampedRows; i++) {
console.log(letters.slice(i, clampedRows));
} letters.slice(i, rows) returns characters from index i up to (but not including) index rows. With rows = 5, row 0 is letters.slice(0, 5) = ABCDE, row 1 is letters.slice(1, 5) = BCDE, and so on. Keep the two-loop version for exams that ask you to show both bounds.
Use prompt() when reading input. Set rows (fixed or from user), clamp to 1–26, and compute end = "A".charCodeAt(0) + rows - 1.
for (let i = 0; i < rows; i++) selects the starting letter for the current line — A on row 0, B on row 1, and so on.
start = base + i then for (let code = start; code <= end; code++): prints each letter with line += String.fromCharCode(code).
console.log(line) ends the row so the next outer iteration starts fresh.
Total letters: n+(n-1)+…+1 = n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 5Trace each outer-loop value i (0-based) and see what the inner loop prints from start through fixed end = E.
Outer i | start | end | Printed row | Letters this row |
|---|---|---|---|---|
0 | A | E | ABCDE | 5 |
1 | B | E | BCDE | 4 |
2 | C | E | CDE | 3 |
3 | D | E | DE | 2 |
4 | E | E | E | 1 |
Total letter prints: 5 + 4 + 3 + 2 + 1 = 15 = 5×6/2. Same triangular total as Programs 1 and 5 — only which letter is fixed differs.
Where this shifting-start letter pattern (and its fixed end bound) shows up beyond the homework prompt.
Clearest visual proof that end = "A".charCodeAt(0) + rows - 1 stays constant while start shifts each row.
Example: compare side-by-side with Program 5.
Natural step after Program 5 before reverse and pyramid letter patterns.
Example: Program 7 reverses letters within each row.
Practice character loops and line += letter/console.log(line) with a shape that differs visibly from Program 5.
Example: swap start/end logic and compare outputs.
Swap to lowercase or digits once the letter loop works.
Example: print lowercase a..z once uppercase clicks.
Triangular totals make O(n²) concrete for beginners.
Example: count printed letters for n = 10 → 55.
Pair the pattern with Number.isFinite validation and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for the left-trim variant, explain that only the start shifts — the end stays at "A".charCodeAt(0) + rows - 1.
Why this shifting-start pattern earns a spot after Program 5 in beginner JavaScript courses.
Side-by-side with Program 5 makes fixed-end vs fixed-start bounds obvious.
Only loops and console output — no arrays or math libraries.
One formula change flips between Program 5’s fixed start and this fixed end.
Streaming output needs no storage beyond loop counters.
Pro Tip: master Program 5 first, then this page — the row count is the same; only which bound moves changes.
Small habits that keep shifting-start alphabet-pattern code clean.
Set end = "A".charCodeAt(0) + rows - 1 before the outer loop — don’t recalculate every row.
Avoid crashes when the user types letters instead of a number.
Only call console.log(line) after the inner loop finishes the row.
code <= end includes the fixed end letter on every row.
Trace rows = 3 on paper — expect ABC, BC, C — before coding larger demos.
Pro Tip: if the last letter is missing on every row, you almost certainly forgot end + 1 in the inner range.
Mistakes that commonly break shifting-start alphabet patterns.
Using code < end stops before the end letter — every row drops its last character.
→ Use code <= end so the fixed end letter logs.
Using end = base + rows or end = base + i produces the wrong last letter.
→ Fixed end is always end = "A".charCodeAt(0) + rows - 1.
Omitting console.log(line) after the inner loop glues every letter onto one endless line.
→ Always end the row after the inner loop.
Non-numeric input raises ValueError with bare parseInt(prompt()).
→ Wrap in Number.isFinite validation and validate range.
Copying Program 5’s logic prints ABCDE, ABCD, ABC — fixed start, not shifting start.
→ This pattern: start = base + i, fixed end = base + rows - 1.
Check these inputs before calling the solution done.
Output is just A — start and end are both A.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n²/2 characters — fine for labs, noisy for huge n.
parseInt(prompt()) raises ValueError — validate first.
On the last row, start == end — inner loop prints one letter only.
Try these variations to lock in the shifting-start pattern.
rowsString.fromCharCode(code) with digit logicNumber.isFinite validation until rows >= 1end = "A".charCodeAt(0) + rows - 1 is computed once — for five rows every row ends at E.code <= end must include + 1 — range stops before its end value.rows > 0 for interactive programs; rows = 1 should print a single A.Quick Takeaway: compute fixed end, shift start each row, print with code <= end, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
letters.slice(i, rows) (Example 3) | O(rows²) | O(rows) per row string (temporary) |
The shifting-start alphabet pattern is a compact bounds exercise with lasting payoff: fixed end, per-row start, and O(n²) intuition. Master the classic two-loop version, then optionally shorten rows with letters.slice(i, rows).
Practice the three examples above, then continue to Program 7 for the reverse-letter variant in the series.
Every row ends at the same letter — keep code <= end, use line += letter for letters and console.log(line) for the break, and validate row counts when reading input.
end = "A".charCodeAt(0) + rows - 1 once before the outer loopstart = "A".charCodeAt(0) + i inside for (let i = 0; i < rows; i++)code <= end and line += String.fromCharCode(code)rows ≥ 1 for interactive programsparseInt(prompt()) in Number.isFinite validation+ 1 on the inner range endrows = 1 edge casePrint ABCDE-to-E the beginner-friendly way.
Fixed end, shifting start
Definitionend = base + rows - 1
Codestart = base + i
Codecode <= end
I/OO(n²) time
AnalysisReverse the letters within each row for the next alphabet pattern in the series.
12 people found this page helpful