Shifting-Start Alphabet Pattern in JavaScript

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops

What You’ll Learn

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.

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.

Outer Loop

Row index

for (let i = 0; i < rows; i++) picks the starting letter for each row (0-based index).

Inner Loop

start..end letters

for (let code = start; code <= end; code++) appends from the row start through the fixed end letter.

line vs console.log

Same line / next line

Letters use line += String.fromCharCode(code); end each row with console.log(line).

Live Preview

1–26 rows

Pick a row count and draw the shifting-start pattern instantly in the browser.

O(n²)

Complexity

Total letters = n(n+1)/2; extra memory stays O(1).

Introduction

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.

Why it matters?

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.

Key Highlights

Fixed End Letter

end = "A".charCodeAt(0) + rows - 1 — for five rows, every row ends at E.

Shifting Start

Row i starts at String.fromCharCode("A".charCodeAt(0) + i) — A, then B, then C, and so on.

Build Then Log

line += String.fromCharCode(code) in the inner loop; console.log(line) after.

Program 5 Mirror

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).

📝 Problem & Approach

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).

JavaScript
# First 5 rows (conceptual shape)
# ABCDE
# BCDE
# CDE
# DE
# E

Inputs & Outputs

ItemTypeDescription
rowsintNumber of pattern lines to print (typically ≥ 1).
endint (code)Fixed last letter: "A".charCodeAt(0) + rows - 1.
Printed outputtextLeft-aligned rows; row i prints from String.fromCharCode("A".charCodeAt(0) + i) through the fixed end letter.

Minimal workflow

Pseudocode
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 comparison

ApproachIdeaBest for
Nested loopsShifting start + fixed endLearning and interviews
Fixed end formulaend = "A".charCodeAt(0) + rows - 1This pattern — shared last letter
letters.slice(i, rows)Slice from row start through fixed widthShorter production-style demos

⚡ Quick Reference

GoalPattern
Fixed end letterend = "A".charCodeAt(0) + rows - 1
Walk each rowfor (let i = 0; i < rows; i++)
Row start letterstart = "A".charCodeAt(0) + i
Print start..end lettersfor (let code = start; code <= end; code++) { line += String.fromCharCode(code); }
End the rowconsole.log(line)
One-line row shortcutconsole.log(letters.slice(i, rows))
Fixed-start variantSee Program 5 — start stays at A

📋 Fixed end vs shifting start vs slice

Same ABCDE-to-E shape — three ways to think about row bounds.

Fixed end
end = base + rows - 1

Every row ends at the same letter — E when rows = 5

Shifting start
start = base + i

Row i drops leading letters — A, then B, then C, and so on

letters.slice(i, rows)
whole row

Builds each row at once — skip the inner loop

Learning tip
loops first

Master nested loops before the string shortcut

Context

When This Pattern Shows Up

Reach for this pattern when teaching fixed end bounds with a shifting start — the mirror of Program 5’s fixed-start shape.

  1. Per-row bounds practice

    Natural follow-up after Program 5 — same row count, opposite trim direction.

  2. Nested-loop warm-up

    Practice code <= end with an immediate visual check.

  3. prompt() input practice

    Combine loops with prompt() for a flexible row count.

  4. Gateway to variants

    Leads to reverse patterns, pyramids, and hollow shapes in the series.

  5. Not a UI layout tool

    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.

🔮 Live Preview

Choose a row count between 1 and 26 and draw the shifting-start alphabet pattern in the browser.

Try 5, 7, or 10. Cap is 26 letters (A–Z).

Live result
Press "Draw pattern".

Examples Gallery

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.

📚 Getting Started

Print five rows with classic nested loops — shifting start, fixed end.

Example 1 — Fixed rows = 5

Hard-coded height — ideal for first demos and screenshots.

JavaScript
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);
}
Try it Yourself

How It Works

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.

📈 Practical Variant

Let the user choose the height at runtime.

Example 2 — User Input Version

Read the row count with prompt() and convert with parseInt() (validate with Number.isFinite in real apps).

JavaScript
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);
}
Try it Yourself

How It Works

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.

⚡ Shortcut Style

Same shape without an explicit inner letter loop.

Example 3 — letters.slice(i, rows)

Slice A–Z from index i through rows for each row.

JavaScript
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));
}
Try it Yourself

How It Works

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.

🧠 How the Algorithm Prints Rows

1

Set up

Use prompt() when reading input. Set rows (fixed or from user), clamp to 1–26, and compute end = "A".charCodeAt(0) + rows - 1.

Setup
2

Outer loop (row index)

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.

Row
3

Inner loop (letters)

start = base + i then for (let code = start; code <= end; code++): prints each letter with line += String.fromCharCode(code).

Letters
4

New line

console.log(line) ends the row so the next outer iteration starts fresh.

Break
=

Shifting-start pattern complete

Total letters: n+(n-1)+…+1 = n(n+1)/2O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — rows = 5

Trace each outer-loop value i (0-based) and see what the inner loop prints from start through fixed end = E.

Outer istartendPrinted rowLetters this row
0AEABCDE5
1BEBCDE4
2CECDE3
3DEDE2
4EEE1

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.

Use Cases

Where this shifting-start letter pattern (and its fixed end bound) shows up beyond the homework prompt.

1. Teaching Fixed End Bounds

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.

2. Pattern Series Bridge

Natural step after Program 5 before reverse and pyramid letter patterns.

Example: Program 7 reverses letters within each row.

3. Console Formatting Drills

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.

4. Character Substitution

Swap to lowercase or digits once the letter loop works.

Example: print lowercase a..z once uppercase clicks.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed letters for n = 10 → 55.

6. Input Validation Labs

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.

Advantages

Why this shifting-start pattern earns a spot after Program 5 in beginner JavaScript courses.

  1. 1. Instant Visual Contrast

    Side-by-side with Program 5 makes fixed-end vs fixed-start bounds obvious.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Compare

    One formula change flips between Program 5’s fixed start and this fixed end.

  4. 4. O(1) Extra Memory

    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.

Usage Tips

Small habits that keep shifting-start alphabet-pattern code clean.

  1. 1. Compute end Once

    Set end = "A".charCodeAt(0) + rows - 1 before the outer loop — don’t recalculate every row.

  2. 2. Validate prompt() input

    Avoid crashes when the user types letters instead of a number.

  3. 3. Keep console.log(line) Outside

    Only call console.log(line) after the inner loop finishes the row.

  4. 4. Remember end + 1

    code <= end includes the fixed end letter on every row.

  5. 5. Dry-Run One Small n

    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.

Common Pitfalls

Mistakes that commonly break shifting-start alphabet patterns.

  1. 1. Stopping inner loop before end

    Using code < end stops before the end letter — every row drops its last character.

    → Use code <= end so the fixed end letter logs.

  2. 2. Wrong end Formula

    Using end = base + rows or end = base + i produces the wrong last letter.

    → Fixed end is always end = "A".charCodeAt(0) + rows - 1.

  3. 3. Forgetting the Row Break

    Omitting console.log(line) after the inner loop glues every letter onto one endless line.

    → Always end the row after the inner loop.

  4. 4. Blind parseInt(prompt())

    Non-numeric input raises ValueError with bare parseInt(prompt()).

    → Wrap in Number.isFinite validation and validate range.

  5. 5. Confusing with Program 5

    Copying Program 5’s logic prints ABCDE, ABCD, ABC — fixed start, not shifting start.

    → This pattern: start = base + i, fixed end = base + rows - 1.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single letter row

Output is just A — start and end are both A.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Many rows

Output grows as n²/2 characters — fine for labs, noisy for huge n.

Bad input

Non-numeric input

parseInt(prompt()) raises ValueError — validate first.

Last row

start equals end

On the last row, start == end — inner loop prints one letter only.

🎯 Practice Problems

Try these variations to lock in the shifting-start pattern.

1. Compare with Program 5

  • Run both patterns with the same rows
  • Note fixed start vs fixed end

2. Print digits instead

  • Replace String.fromCharCode(code) with digit logic
  • Same shifting start structure

3. Safe input loop

  • Use Number.isFinite validation until rows >= 1
  • Then draw the shifting-start pattern

4. Continue the series

  • Try Program 7 — reverse letters each row
  • Next step after ABCDE-to-E

Notes

  • Fixed end. end = "A".charCodeAt(0) + rows - 1 is computed once — for five rows every row ends at E.
  • code <= end must include + 1range stops before its end value.
  • Validate rows > 0 for interactive programs; rows = 1 should print a single A.
  • Program 5 trims from the end; this pattern trims from the start — compare both to see how bounds drive the shape.

Quick Takeaway: compute fixed end, shift start each row, print with code <= end, then break the line.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(1)
letters.slice(i, rows) (Example 3)O(rows²)O(rows) per row string (temporary)
Wrap Up

🎉 Conclusion

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.

💡 Best Practices

✅ Do

  • Compute end = "A".charCodeAt(0) + rows - 1 once before the outer loop
  • Use start = "A".charCodeAt(0) + i inside for (let i = 0; i < rows; i++)
  • Use code <= end and line += String.fromCharCode(code)
  • Validate rows ≥ 1 for interactive programs
  • Wrap parseInt(prompt()) in Number.isFinite validation
  • State O(n²) time when asked about complexity

❌ Don’t

  • Forget + 1 on the inner range end
  • Use Program 5’s fixed-start logic for this shape
  • Skip the newline after each row
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this shifting-start pattern

Print ABCDE-to-E the beginner-friendly way.

5
Core concepts
E 02

Fixed end

end = base + rows - 1

Code
A 03

Shifting start

start = base + i

Code
04

range + console.log(line)

code <= end

I/O
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

The outer loop walks i from 0 to rows-1. Each row sets start = "A".charCodeAt(0) + i and end = "A".charCodeAt(0) + rows - 1. The inner loop appends from start through end, so row 0 is ABCDE, row 1 is BCDE, and the last row is a single E.
For rows = 5, end is the code for E — the last letter on every row. The first row spans A through E; each next row drops the leading letter but keeps the same end letter.
A for loop with code <= end includes the final letter on each row. Stopping at end - 1 drops the last character (e.g. E on the last row).
Program 5 keeps start at A and shrinks the end each row (ABCDE, ABCD, ...). This pattern keeps a fixed end and shifts the start right each row (ABCDE, BCDE, ...).
O(n²) where n is the number of rows. Total logged characters equal n+(n-1)+...+1 = n(n+1)/2 — same triangular count as Programs 1 and 5.
Yes. Keep letters = "ABCDEFG..." and console.log(letters.slice(i, rows)) for each row index i. Nested charCode loops teach the bounds; slice is a compact shortcut.
Use parseInt with Number.isFinite after prompt(), then clamp rows between 1 and 26 so letter codes stay within A–Z.
Letter codes walk past Z and log unexpected characters. Clamp to 26 for A–Z demos, or define a clear error/wrap policy.

Did you Know? 🔊

Each row logs from a row-specific start letter through a fixed end: end = "A".charCodeAt(0) + rows - 1. Row i uses start = "A".charCodeAt(0) + i, so rows shrink from ABCDE to E. Compare Program 5 (fixed start A, shrinking end) and Program 3 (fixed top letter rows).

Continue to Program 7

Reverse the letters within each row for the next alphabet pattern in the series.

Program 7 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful