JavaScript Alphabet Triangle Pattern (Odd Length)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An odd-length alphabet triangle prints a left-aligned staircase where each row is the prefix A…end, and the ending letter jumps by two so row widths stay odd: 1, 3, 5, 7, …

Remember
Rule: row ends at A, C, E, G, I…; always restart at A

A
ABC
ABCDE
ABCDEFG
ABCDEFGHI     ← 5 rows (ends at I)

Contrast Program 1 (step 1: A, AB, ABC…). Here the outer loop uses i += 2; the inner loop still walks from A through the current end letter.

How to Solve It

Drive the pattern from an ending letter (i += 2) or from a row count (end = base + 2*(row-1)).

MethodIdeaBest for
Step-2 end letterfor (i = base; i <= last; i += 2), then print A…iLearning the odd-step idea
Row countend = base + 2*(row-1) for each rowUser enters how many rows

Pseudocode

Pseudocode
base = code of 'A'
for row from 1 to rows:
    end = base + 2 * (row - 1)
    line = ""
    for code from base to end:
        append fromCharCode(code) to line
    print line

Cheat sheet

GoalPattern
Odd end lettersfor (let i = base; i <= last; i += 2)
End for row rconst end = base + 2 * (row - 1);
Print prefixfor (let j = base; j <= end; j++) line += String.fromCharCode(j);
End the rowconsole.log(line);
Letter total1+3+…+(2r-1) = r²
Stay in A–YClamp rows to 1…13 (end letter ≤ Y)
Step-1 twinWidths 1, 2, 3… → Program 1

Printing Letters vs Starting a New Line

APIEffectUse for
line += chStays on the same rowEach letter in the prefix
console.log(line)Ends the current rowAfter the inner loop

Append letters without a newline, then end the row once.

Live Preview

Change the row count and the odd-length triangle updates instantly — including the end letter and perfect-square total.

Whole numbers from 1 to 7 in this preview (up to 13 keeps end ≤ Y). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · end I · 25 letters
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI

Worked Walkthrough — rows = 4

Trace each row’s end letter and the prefix printed from A.

rowendPrinted rowLetters
1AA1
2CABC3
3EABCDE5
4GABCDEFG7

Total letter characters: 1 + 3 + 5 + 7 = 16 = 4². Odd widths sum to a perfect square — that is why time is O(r²).

JavaScript Programs

Three complete programs: fixed ending letter, prompt for ending letter, and a row-count style. Use View Output for sample results, or Try It Yourself to edit and run in the playground.

Example 1 — Fixed through 'I'

Hard-coded ending letter — ideal for first demos and screenshots. Five odd-length rows.

JavaScript
const base = "A".charCodeAt(0);
const last = "I".charCodeAt(0);

for (let i = base; i <= last; i += 2) {
  let line = "";
  for (let j = base; j <= i; j++) {
    line += String.fromCharCode(j);
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Set bounds. base is A; last is I (five odd steps: A, C, E, G, I).

2. Outer loop steps by 2. i takes each end letter. i += 2 keeps every row width odd.

3. Inner loop prints the prefix. j always restarts at base and walks through i.

4. Break the line. console.log(line) after the inner loop prints the row and starts the next one.

Example 2 — Ending Letter Input

Read an odd-step ending letter (A, C, E, …) at runtime. Validate a single A–Z character.

JavaScript
const raw = (prompt("Enter the ending letter (odd step like I):") || "").trim().toUpperCase();
const base = "A".charCodeAt(0);

if (!/^[A-Z]$/.test(raw)) {
  console.log("Please enter a single letter A-Z.");
} else {
  let end = raw.charCodeAt(0);
  if ((end - base) % 2 !== 0) end -= 1; // snap even letters down (B→A, D→C, …)
  if (end > "Y".charCodeAt(0)) end = "Y".charCodeAt(0);

  for (let i = base; i <= end; i += 2) {
    let line = "";
    for (let j = base; j <= i; j++) {
      line += String.fromCharCode(j);
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and normalize. Trim, uppercase, and require a single A–Z letter.

2. Keep an odd step. If the user typed an even letter (B, D, …), snap down by one so ends stay A, C, E, …

3. Same nested-loop core. Only the outer upper bound changes — the print logic matches Example 1.

Example 3 — end = base + 2*(row-1)

Drive the pattern from a row count — clearer when the user enters how many rows to print.

JavaScript
let rows = 5;
const base = "A".charCodeAt(0);

for (let row = 1; row <= rows; row++) {
  const end = base + 2 * (row - 1);
  let line = "";
  for (let j = base; j <= end; j++) {
    line += String.fromCharCode(j);
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop is a plain row counter. row runs from 1 to rows.

2. Compute the end letter. Row 1 ends at base + 0, row 2 at base + 2, row 3 at base + 4, and so on.

3. Print A through end. Same inner prefix loop as Examples 1–2.

Clamp rows to 1…13 so end stays within A–Y.

Edge Cases & Pitfalls

Check these before calling the solution done.

step 1

Looks like Program 1

If you use i++ instead of i += 2, you get widths 1, 2, 3… — that is Program 1.

even end

Messy odd/even mix

Prefer ending letters A, C, E, …, Y. Even letters (B, D, …) break the clean odd-length progression from the first row.

log inside

Column of letters

If console.log is inside the inner loop, each letter lands on its own line. Append with +=; call console.log only after the inner loop.

Reuse line

Growing leftovers

Reset line = "" at the start of each outer iteration, or letters from previous rows stick around.

rows > 13

Past Y

Clamp to 13 rows so end stays ≤ Y. Beyond that, codes leave the A–Z range for this odd-step pattern.

rows = 1

Single A

Output is just A on one line — a good sanity check.

Time and Space Complexity

ProgramTimeExtra space
Step-2 / ending letter (Examples 1–2)O(r²)O(r) for the current line string
Row count (Example 3)O(r²)O(r) for the current line string

Total letters logged = 1 + 3 + … + (2r-1) = r², so time is quadratic in the number of rows.

Key Takeaways

  • Rule: each row is A…end with end letters A, C, E, G… (odd widths).
  • Step 2: use i += 2 or end = base + 2*(row-1).
  • Break the row: call console.log(line) only after the inner loop.
  • Complexity: O(r²) time because letter totals form a perfect square.

One line: for each odd end letter, print A through that letter, then console.log.

Frequently Asked Questions

It makes the ending letter jump by two (A, C, E, …) so each row length increases by two characters and stays odd. In JavaScript use for (let i = base; i <= end; i += 2).
The outer loop advances the ending letter by 2 (A, C, E, G, I). The inner loop appends every letter from A through that ending letter, so the count is always odd.
Because each row is a fresh prefix A…end. Starting at the end letter would skip earlier letters and change the pattern.
line += String.fromCharCode(j) builds the full row string. console.log() inside the inner loop would log one letter per line. Log once after the inner loop finishes.
1+3+…+(2r−1)=r². For 5 rows that is 25 letters.
O(r²) for r rows, because total logged letters equal r².
The loop still runs, but you no longer get a clean set of odd-length rows aligned to A, C, E, …. Prefer an odd-step ending letter (A, C, E, …, Y) for this pattern.
Prefer reading a row count with parseInt and Number.isFinite after prompt(), or validate a single A–Z letter from prompt().trim().toUpperCase(), and keep the ending letter within A–Y.

Did you know?

Odd numbers add up to perfect squares: 1+3+5+…+(2r-1)=r². That is why this pattern logs exactly r² letters for r rows — the same count that makes the complexity O(r²).

Next: Symmetric Alphabet with Star Center

Mirrored letters with stars filling the middle — ABCDEEDCBA, ABCD**DCBA…

Program 15 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