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.
Approach
How to Solve It
Drive the pattern from an ending letter (i += 2) or from a row count (end = base + 2*(row-1)).
Method
Idea
Best for
Step-2 end letter
for (i = base; i <= last; i += 2), then print A…i
Learning the odd-step idea
Row count
end = base + 2*(row-1) for each row
User 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
Goal
Pattern
Odd end letters
for (let i = base; i <= last; i += 2)
End for row r
const end = base + 2 * (row - 1);
Print prefix
for (let j = base; j <= end; j++) line += String.fromCharCode(j);
Append letters without a newline, then end the row once.
Try it
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 result5 rows · end I · 25 letters
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
Trace
Worked Walkthrough — rows = 4
Trace each row’s end letter and the prefix printed from A.
row
end
Printed row
Letters
1
A
A
1
2
C
ABC
3
3
E
ABCDE
5
4
G
ABCDEFG
7
Total letter characters: 1 + 3 + 5 + 7 = 16 = 4². Odd widths sum to a perfect square — that is why time is O(r²).
Code
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);
}
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);
}
}
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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²).