Shape Rule
Odd widths 1, 3, 5…
Each row prints the prefix A..end where end is A, C, E, G, I.

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.
Odd widths 1, 3, 5…
Each row prints the prefix A..end where end is A, C, E, G, I.
Step by 2
for (let i = "A".charCodeAt(0); i <= "I".charCodeAt(0); i += 2) picks the end letter.
Print A..i
for (let j = "A".charCodeAt(0); j <= i; j++) restarts at A every row.
Same line / next line
Letters use line += ch; end each row with console.log(line).
1–13 rows
Pick a row count and draw the odd-length triangle in the browser.
Complexity
Total letters = r²; extra memory stays O(1).
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.
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.
Row lengths are 1, 3, 5, 7, 9, …
Outer end letter jumps with i += 2.
Inner loop always restarts at A.
Odd sum identity: total prints equal r².
In short: for each end letter i stepping A, C, E, …, print A..i with line += String.fromCharCode(j), then call console.log(line).
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.
// First 5 rows (conceptual shape)
// A
// ABC
// ABCDE
// ABCDEFG
// ABCDEFGHI | Item | Type | Description |
|---|---|---|
rows / end letter | int / char | Number of odd-length lines (1–13 for A–Y), or last end letter such as 'I'. |
| Printed output | text | Left-aligned rows; row k prints letters from A through 'A' + 2*(k-1). |
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 | Idea | Best for |
|---|---|---|
i += 2 / end = base + 2*(row-1) | Outer end letter steps by two | Learning and interviews |
| Row index formula | end = "A".charCodeAt(0) + 2 * (row - 1) | Clearer when input is a row count |
| Goal | Pattern |
|---|---|
| Step end letters | for (let i = "A".charCodeAt(0); i <= "I".charCodeAt(0); i += 2) |
| Print prefix A..i | for (j = 'A'; j <= i; j++) line += String.fromCharCode(j) |
| End the row | console.log(line) |
| End from row index | end = "A".charCodeAt(0) + 2 * (row - 1) |
| Step-1 triangle | See Program 1 (A, AB, ABC, …) |
Same triangle - different roles for each tool.
same linePrints a letter without moving to the next line
new lineEnds the current row after the prefix is printed
odd endsJumps the ending letter A → C → E …
step +2In JS, use i += 2 / end = base + 2*(row-1)
Reach for this triangle when practicing loop steps and odd-width prefixes.
Change only the outer step from 1 to 2 for odd widths.
Practice += 2 on chars and int row formulas.
Odd sums equal squares - count printed letters for small r.
Next: symmetric alphabet rows with a star center.
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.
Choose a row count between 1 and 13 and draw the odd-length alphabet triangle in the browser.
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.
Print five odd-length rows with a step-2 end letter.
'I'Hard-coded ending letter - ideal for first demos and screenshots.
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);
} 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.
Let the user choose the last ending letter.
Read an odd-step ending letter (A, C, E, …). Prefer validating a single A-Z character in real apps.
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);
} 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.
Drive the pattern from a row count instead of an ending letter.
end = base + 2*(row-1)Clear when the user enters how many rows to print.
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);
} 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.
Use prompt() when reading input. Choose a last end letter or a row count.
i takes A, C, E, G, I via for (let i = base; i <= last; i += 2).
j always starts at A and prints every letter up to the current i.
console.log(line) ends the row so the next outer iteration starts fresh.
Total letters: 1+3+…+(2r-1) = r² — O(r²) time, O(1) extra memory.
'I'Trace each outer-loop value of i and count how many letters the inner loop prints.
i | Inner j range | Printed row | Length |
|---|---|---|---|
'A' | 'A'..'A' | A | 1 |
'C' | 'A'..'C' | ABC | 3 |
'E' | 'A'..'E' | ABCDE | 5 |
'G' | 'A'..'G' | ABCDEFG | 7 |
'I' | 'A'..'I' | ABCDEFGHI | 9 |
Total letter prints: 1 + 3 + 5 + 7 + 9 = 25 = 5².
Where this tiny pattern (and its step-by-2 idea) shows up beyond the homework prompt.
Clearest demo that the outer increment controls width growth.
Example: change += 2 to += 1 and watch Program 1 appear.
Teach step size as a one-line difference between patterns.
Example: side-by-side A/AB/ABC vs A/ABC/ABCDE.
Count letters to see that odd totals equal squares.
Example: 5 rows → 25 = 5² prints.
Lowercase or spaced letters once the loops work.
Example: start from 'a' with the same += 2.
Square totals make O(r²) concrete without triangular formulas.
Example: r = 10 → 100 letter prints.
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.
Why this pattern earns a spot right after the classic A/AB/ABC triangle.
Wrong step size shows up immediately as consecutive widths instead of odd ones.
Only nested loops and a step of 2 - no arrays required.
Flip back to Program 1 by changing the outer step to 1.
Total work is exactly r² - memorable for interviews.
Pro Tip: learn the step-2 end-letter version first; treat the row-index formula as an equivalent rewrite afterward.
Small habits that keep odd-length alphabet code clean.
Use i += 2 or end = base + 2*(row-1) - do not use step 1 by accident.
Use A, C, E, …, Y when you want clean odd lengths from row 1.
Inner loop must begin at 'A' every row for this prefix shape.
Row 13 ends at Y; row 14 would leave A–Z.
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.
Mistakes that commonly break odd-length alphabet patterns.
You get Program 1’s consecutive widths (A, AB, ABC, …).
→ Keep i += 2 or end = base + 2*(row-1).
iSkipping A produces single letters or wrong prefixes.
→ Always restart the inner loop at "A".charCodeAt(0).
Using i++ (step 1) recreates Program 1’s A, AB, ABC shape.
→ Keep i += 2 or end = base + 2*(row-1).
Empty tokens or non-letters produce unexpected ending letters.
→ Validate a single A–Z letter, or take a row count with Number.isFinite.
Beyond 13 rows the end letter leaves A–Z.
→ Clamp to 1–13 or stop when end > 'Z'.
Check these inputs before calling the solution done.
Output is just A on one line.
Prints A / ABC / ABCDE.
Still runs, but odd-length alignment from A is messier - prefer odd-step ends.
End letter Y; 13² = 169 prints.
Validate before taking the first character of the input string.
Same loops work with 'a' and += 2.
Try these variations to lock in the pattern.
r²r² - hence O(r²) time.A.i += 2 or the row-index end formula.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.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1-3) | O(r²) | O(1) |
Because 1+3+…+(2r-1)=r², the letter count is exactly a perfect square.
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 r².
i += 2 or the row-index end formula'A' every rowr² when asked about complexityPrint the odd-length triangle the beginner-friendly way.
Odd widths via step 2
DefinitionEnd letters A, C, E…
CodePrints A..end each row
CodeEnds each row
I/OO(r²) time
AnalysisOdd 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 up: symmetric alphabet rows with stars filling the center.
12 people found this page helpful