Odd-Length Alphabet Triangle in JavaScript

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

What You’ll Learn

Each row is a block of letters from A through the next “odd step” in the alphabet: A, ABC, ABCDE, ABCDEFG, ABCDEFGHI. The outer loop steps the end letter by 2 (A, C, E, G, I) with i += 2. Compare Program 1 (step 1) and Program 13 (running counter). Includes a live preview, worked JavaScript examples, edge cases, and complexity.

Shape Rule

Odd widths 1, 3, 5…

Each row prints the prefix A..end where end is A, C, E, G, I.

Outer Loop

Step by 2

for (let i = "A".charCodeAt(0); i <= "I".charCodeAt(0); i += 2) picks the end letter.

Inner Loop

Print A..i

for (let j = "A".charCodeAt(0); j <= i; j++) restarts at A every row.

line vs console.log

Same line / next line

Letters use line += ch; end each row with console.log(line).

Live Preview

1–13 rows

Pick a row count and draw the odd-length triangle in the browser.

O(r²)

Complexity

Total letters = ; extra memory stays O(1).

Introduction

An odd-length alphabet triangle grows by two letters on each new line. Every row still starts at A, but the ending letter jumps A → C → E → G → I, so widths are 1, 3, 5, 7, 9.

In JavaScript you usually solve it with two nested for loops: the outer loop steps the end letter by 2, the inner loop prints A through that end letter, then console.log(line) moves to the next line.

Why it matters?

It shows that changing only the outer step (1 vs 2) transforms Program 1 into an odd-width triangle - and that odd-number sums equal perfect squares, which makes complexity analysis concrete.

Key Highlights

Odd Widths

Row lengths are 1, 3, 5, 7, 9, …

Step by Two

Outer end letter jumps with i += 2.

Fresh Prefix

Inner loop always restarts at A.

r² Letters

Odd sum identity: total prints equal .

In short: for each end letter i stepping A, C, E, …, print A..i with line += String.fromCharCode(j), then call console.log(line).

📝 Problem & Approach

Given a row count r (or a fixed odd-step ending letter like 'I'), print a left-aligned triangle of alphabet prefixes with odd lengths.

JavaScript
// First 5 rows (conceptual shape)
// A
// ABC
// ABCDE
// ABCDEFG
// ABCDEFGHI

Inputs & Outputs

ItemTypeDescription
rows / end letterint / charNumber of odd-length lines (1–13 for A–Y), or last end letter such as 'I'.
Printed outputtextLeft-aligned rows; row k prints letters from A through 'A' + 2*(k-1).

Minimal workflow

Pseudocode
for i from "A".charCodeAt(0) to end step 2:
    for j from "A".charCodeAt(0) to i:
        print String.fromCharCode(j) (no newline)
    console.log the row

Approach comparison

ApproachIdeaBest for
i += 2 / end = base + 2*(row-1)Outer end letter steps by twoLearning and interviews
Row index formulaend = "A".charCodeAt(0) + 2 * (row - 1)Clearer when input is a row count

⚡ Quick Reference

GoalPattern
Step end lettersfor (let i = "A".charCodeAt(0); i <= "I".charCodeAt(0); i += 2)
Print prefix A..ifor (j = 'A'; j <= i; j++) line += String.fromCharCode(j)
End the rowconsole.log(line)
End from row indexend = "A".charCodeAt(0) + 2 * (row - 1)
Step-1 triangleSee Program 1 (A, AB, ABC, …)

📋 line vs console.log vs step-2

Same triangle - different roles for each tool.

line += ch
same line

Prints a letter without moving to the next line

console.log(line)
new line

Ends the current row after the prefix is printed

i += 2
odd ends

Jumps the ending letter A → C → E …

Learning tip
step +2

In JS, use i += 2 / end = base + 2*(row-1)

Context

When This Pattern Shows Up

Reach for this triangle when practicing loop steps and odd-width prefixes.

  1. After Program 1

    Change only the outer step from 1 to 2 for odd widths.

  2. Step-size drills

    Practice += 2 on chars and int row formulas.

  3. Math intuition labs

    Odd sums equal squares - count printed letters for small r.

  4. Gateway to Program 15

    Next: symmetric alphabet rows with a star center.

  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 links loop step size, odd widths, and the classic odd-sum = square identity.

🔮 Live Preview

Choose a row count between 1 and 13 and draw the odd-length alphabet triangle in the browser.

Try 5 (through I) or 3 (through E). Max 13 keeps the last end letter at Y.

Live result
Press "Draw pattern".

Examples Gallery

Three complete JavaScript programs - fixed ending letter, prompt for ending letter, and a row-count driven variant. Click View Output to reveal sample console results, or Try it Yourself to run in the browser editor.

📚 Getting Started

Print five odd-length rows with a step-2 end letter.

Example 1 — Fixed through 'I'

Hard-coded ending letter - ideal for first demos and screenshots.

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

When i is "A".charCodeAt(0), the inner loop builds A. When i is "C".charCodeAt(0), it builds ABC, and so on through ABCDEFGHI. Step end letters with i += 2 so each row length stays odd.

📈 Practical Variant

Let the user choose the last ending letter.

Example 2 — Ending Letter Input

Read an odd-step ending letter (A, C, E, …). Prefer validating a single A-Z character in real apps.

JavaScript
const raw = (prompt("Enter the ending letter (odd step like I):") || "").trim().toUpperCase();
const end = raw ? raw.charCodeAt(0) : "I".charCodeAt(0);
const base = "A".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

Same nested-loop core as Example 1; only the outer upper bound changes. Prefer odd-step endings (A, C, E, …) so every row length stays odd from the start.

⚡ Row-Count Style

Drive the pattern from a row count instead of an ending letter.

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

Clear when the user enters how many rows to print.

JavaScript
const 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

Row 1 ends at base + 0, row 2 at base + 2, row 3 at base + 4, and so on. Clamp rows to 1-13 so end stays within A-Y.

🧠 How the Algorithm Prints Rows

1

Set up

Use prompt() when reading input. Choose a last end letter or a row count.

Setup
2

Outer loop (end letter)

i takes A, C, E, G, I via for (let i = base; i <= last; i += 2).

Odd steps
3

Inner loop (prefix)

j always starts at A and prints every letter up to the current i.

A..i
4

New line

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

Break
=

Triangle complete

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

🔎 Worked Walkthrough — through 'I'

Trace each outer-loop value of i and count how many letters the inner loop prints.

iInner j rangePrinted rowLength
'A''A'..'A'A1
'C''A'..'C'ABC3
'E''A'..'E'ABCDE5
'G''A'..'G'ABCDEFG7
'I''A'..'I'ABCDEFGHI9

Total letter prints: 1 + 3 + 5 + 7 + 9 = 25 = .

Use Cases

Where this tiny pattern (and its step-by-2 idea) shows up beyond the homework prompt.

1. Loop Step Practice

Clearest demo that the outer increment controls width growth.

Example: change += 2 to += 1 and watch Program 1 appear.

2. Contrast with Program 1

Teach step size as a one-line difference between patterns.

Example: side-by-side A/AB/ABC vs A/ABC/ABCDE.

3. Odd-Sum Identity

Count letters to see that odd totals equal squares.

Example: 5 rows → 25 = 5² prints.

4. Case & Spacing Variants

Lowercase or spaced letters once the loops work.

Example: start from 'a' with the same += 2.

5. Complexity Intuition

Square totals make O(r²) concrete without triangular formulas.

Example: r = 10 → 100 letter prints.

6. Input Style Labs

Practice both ending-letter and row-count APIs for the same shape.

Example: map rows=3 ↔ end='E'.

Pro Tip: say “outer picks the odd end letter, inner prints A through that end” before coding - that story prevents forgetting to restart at A.

Advantages

Why this pattern earns a spot right after the classic A/AB/ABC triangle.

  1. 1. Instant Visual Feedback

    Wrong step size shows up immediately as consecutive widths instead of odd ones.

  2. 2. Minimal Concepts

    Only nested loops and a step of 2 - no arrays required.

  3. 3. Easy to Mirror

    Flip back to Program 1 by changing the outer step to 1.

  4. 4. Clean Complexity Story

    Total work is exactly - memorable for interviews.

Pro Tip: learn the step-2 end-letter version first; treat the row-index formula as an equivalent rewrite afterward.

Usage Tips

Small habits that keep odd-length alphabet code clean.

  1. 1. Name the Step Clearly

    Use i += 2 or end = base + 2*(row-1) - do not use step 1 by accident.

  2. 2. Prefer Odd Endings

    Use A, C, E, …, Y when you want clean odd lengths from row 1.

  3. 3. Always Restart at A

    Inner loop must begin at 'A' every row for this prefix shape.

  4. 4. Clamp to 13 Rows

    Row 13 ends at Y; row 14 would leave A–Z.

  5. 5. Dry-Run One Small r

    Trace 3 rows (A / ABC / ABCDE) on paper before coding larger demos.

Pro Tip: if you get A, AB, ABC instead of A, ABC, ABCDE, you used step 1 instead of step 2.

Common Pitfalls

Mistakes that commonly break odd-length alphabet patterns.

  1. 1. Using Step 1 Instead of Step 2

    You get Program 1’s consecutive widths (A, AB, ABC, …).

    → Keep i += 2 or end = base + 2*(row-1).

  2. 2. Starting the Inner Loop at i

    Skipping A produces single letters or wrong prefixes.

    → Always restart the inner loop at "A".charCodeAt(0).

  3. 3. Forgetting the Step of 2

    Using i++ (step 1) recreates Program 1’s A, AB, ABC shape.

    → Keep i += 2 or end = base + 2*(row-1).

  4. 4. Blind First-Character Read

    Empty tokens or non-letters produce unexpected ending letters.

    → Validate a single A–Z letter, or take a row count with Number.isFinite.

  5. 5. Too Many Rows

    Beyond 13 rows the end letter leaves A–Z.

    → Clamp to 1–13 or stop when end > 'Z'.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single letter

Output is just A on one line.

end = E

Three rows

Prints A / ABC / ABCDE.

Even end

Like B or D

Still runs, but odd-length alignment from A is messier - prefer odd-step ends.

rows = 13

Last A–Z fit

End letter Y; 13² = 169 prints.

Bad input

Empty / multi-char

Validate before taking the first character of the input string.

Case

Lowercase variant

Same loops work with 'a' and += 2.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Flip to Program 1

2. Even-length cousin

  • Start at B and step by 2
  • Widths 2, 4, 6, …

3. Count the letters

  • Verify total prints equal
  • Great interview talking point

4. Continue to Program 15

Notes

  • Square count. Total letters for r rows is - hence O(r²) time.
  • Outer step picks the end letter; inner loop always restarts at A.
  • In JS, use i += 2 or the row-index end formula.
  • Clamp to 13 rows (end = Y) for A–Z-only demos.

Quick Takeaway: outer loop steps the end letter by 2, inner loop prints A through that end, then break the line - that is the whole pattern.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1-3)O(r²)O(1)

Because 1+3+…+(2r-1)=r², the letter count is exactly a perfect square.

Wrap Up

🎉 Conclusion

The odd-length alphabet triangle is a small nested-loop exercise with lasting payoff: outer step size, prefix printing, and the odd-sum = square identity. Master the step-2 end-letter version, then optionally drive it from a row count with end = "A".charCodeAt(0) + 2 * (row - 1).

Practice the three examples above, then continue to Program 15’s symmetric alphabet-with-stars pattern.

Step the end letter by 2, always restart the inner loop at A, and remember total prints equal .

💡 Best Practices

✅ Do

  • Use i += 2 or the row-index end formula
  • Restart the inner loop at 'A' every row
  • Prefer odd-step ending letters for clean odd widths
  • Clamp to 1–13 rows for A–Z demos
  • State that total prints equal when asked about complexity

❌ Don’t

  • Use step 1 when you meant odd widths
  • Start the inner loop at the end letter
  • Forget the step of 2 (rows become A, AB, ABC, …)
  • Ignore validation on ending-letter input
  • Allow more than 13 rows without a past-Z policy

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the odd-length triangle the beginner-friendly way.

5
Core concepts
+2 02

Outer loop

End letters A, C, E…

Code
A 03

Inner loop

Prints A..end each row

Code
04

console.log(line)

Ends each row

I/O
05

Complexity

O(r²) time

Analysis

❓ 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^2. For 5 rows that is 25 letters.
O(r^2) for r rows, because total logged letters equal r^2.
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 letter from prompt().trim().toUpperCase(), and keep the ending letter within A-Z.

Did you Know? 🔊

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

Continue to Alphabet Pattern 15

Next up: symmetric alphabet rows with stars filling the center.

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