A rotating alphabet pattern reprints the same letter set each row, starting one letter further along and wrapping earlier letters in reverse so every line stays the same length.
Remember
Rule: for start i, print i..top, then reverse wrap of A..(i-1)
ABCDE
BCDEA
CDEBA
DECBA
EDCBA ← top = 'E' (5 letters each row)
Not a pure left rotation: after BCDEA you get CDEBA (reverse wrap), not CDEAB. Append k - 1 on the wrap loop so the row-start letter is not duplicated.
Approach
How to Solve It
Two ways to emit the same rows — nested char-code loops, then optionally a suffix + reverse-prefix rewrite.
Method
Idea
Best for
Forward + wrap
Append i..top, then wrap with k - 1
Learning, interviews, exams
String slice
Suffix from i + reverse of prefix
Readable rewrite once the idea clicks
Pseudocode
Pseudocode
for i from 'A' to top:
for j from i to top:
append j
for k from i down to 'B':
append (k - 1)
print newline
Cheat sheet
Goal
Pattern
Letter codes
const base = "A".charCodeAt(0);
Row starts
for (let i = base; i <= top; i++)
Forward run
for (let j = i; j <= top; j++) line += String.fromCharCode(j);
Reverse wrap
for (let k = i; k > base; k--) line += String.fromCharCode(k - 1);
End the row
console.log(line);
Row length
top - base + 1 (same on every row)
Avoid double start
Append k - 1, not k, on the wrap loop
Printing Letters vs Starting a New Line
API
Effect
Use for
line += ch
Stays on the same row
Each letter on forward and wrap
console.log(line)
Ends the current row
After both inner loops
Append letters without a newline, then end the row once.
Try it
Live Preview
Change the top letter and the rotating rows update instantly — every line stays the same length.
One letter from A to F. Tap a chip or type a letter — the preview redraws as you go.
Live resultTop E · 5 rows · 5 letters/row
ABCDE
BCDEA
CDEBA
DECBA
EDCBA
Trace
Worked Walkthrough — A–E
Trace each row’s forward run, reverse wrap, and full line.
i
Forward
Wrap (rev)
Printed row
0 (A)
ABCDE
(empty)
ABCDE
1 (B)
BCDE
A
BCDEA
2 (C)
CDE
BA
CDEBA
3 (D)
DE
CBA
DECBA
4 (E)
E
DCBA
EDCBA
Every row length is 5. Note the wrap is the reverse of the prefix — row 3 is CDEBA, not CDEAB.
Code
JavaScript Programs
Three complete programs: fixed A–E, top-letter prompt, and a string-slice rewrite. Use View Output for sample results, or Try It Yourself to edit and run in the playground.
Example 1 — Fixed A–E
Forward run i..E plus wrap run using k - 1.
JavaScript
const base = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = base; i <= top; i++) {
let line = "";
for (let j = i; j <= top; j++) {
line += String.fromCharCode(j);
}
for (let k = i; k > base; k--) {
line += String.fromCharCode(k - 1);
}
console.log(line);
}
2. Outer loop picks the start.i runs from A to E — one start letter per row.
3. Forward run. Append letters from i through top.
4. Reverse wrap. Count k down from i and append k - 1 so the start letter is not repeated.
When i is C, forward builds CDE and wrap builds BA → CDEBA.
Example 2 — Top Letter Input
Same forward + wrap rules with a prompt. Prefer validating a single A–Z character.
JavaScript
const raw = (prompt("Enter top letter (like E):") || "").trim().toUpperCase();
const base = "A".charCodeAt(0);
if (!/^[A-Z]$/.test(raw)) {
console.log("Please enter a single letter A-Z.");
} else {
const top = raw.charCodeAt(0);
for (let i = base; i <= top; i++) {
let line = "";
for (let j = i; j <= top; j++) {
line += String.fromCharCode(j);
}
for (let k = i; k > base; k--) {
line += String.fromCharCode(k - 1);
}
console.log(line);
}
}
1. Slice the suffix.letters.slice(i) is the forward run from the start index.
2. Reverse the prefix. Letters before i are reversed and appended — same wrap as Examples 1–2.
3. One console.log. Concatenate and print the full row in a single call.
For i = 2, forward is CDE and reversed prefix is BA → CDEBA.
Edge Cases & Pitfalls
Check these before calling the solution done.
Append k
Duplicate start letter
If the wrap loop appends k instead of k - 1, rows repeat the start. Always use k - 1.
Forward wrap
Pure left rotation
Appending the prefix in order gives CDEAB, not CDEBA. This pattern reverses the wrap.
log inside
Column of letters
If console.log is inside either inner loop, each letter lands on its own line. Append with +=; log only after both loops.
top = A
Single letter
Output is just A — wrap never runs. A good sanity check.
top < A
Empty output
Outer loop never runs if top < base. Validate A–Z before looping.
Bad input
Validate one letter
Trim, uppercase, and require length 1 in A–Z — reject empty or multi-character prompts.
Analysis
Time and Space Complexity
Program
Time
Extra space
Forward + wrap (Examples 1–2)
O(n²)
O(n) for the current row string
String slice (Example 3)
O(n²)
O(n) per temporary row string
For n = top - 'A' + 1 letters, each of n rows prints n characters — still quadratic in n.
Remember
Key Takeaways
Rule: for start i, append i..top, then reverse wrap of A..(i-1).
No duplicate start: wrap with k - 1, not k.
Break the row: call console.log(line) only after both loops.
Complexity:O(n²) time; O(n) space for the current row string.
One line: for each start i, append i..top, then the reverse of A..(i-1), then console.log(line).
Frequently Asked Questions
The first loop appends from the row start through E (or your top letter). The second appends the letters before the start (wrapping) in reverse using k-1 so the boundary letter is not duplicated.
When k equals i, appending k would repeat the first letter of the row. Appending k-1 starts the wrap with the previous letter (e.g. BCDE then A gives BCDEA).
Each row builds from the row start to the end, then appends earlier letters in reverse to complete the row. That creates the rotation effect.
Yes. Forward length plus wrap length equals E-A+1, so each row has the same number of letters.
Not exactly. A pure left rotation of ABCDE would give BCDEA, CDEAB, DEABC, EABCD. This pattern wraps earlier letters in reverse, so you get CDEBA, DECBA, EDCBA.
line += builds the full row string. console.log inside either inner loop would log one letter per line. Log once after both loops finish.
For n letters, each of n rows prints n characters, so O(n²).
Use prompt(), trim, toUpperCase(), require a single A–Z character, and reject empty or multi-character input.
🤔
Did you know?
Outer i is the row start. First inner loop appends i through E. Second inner loop wraps by counting down from i and appending k - 1 (not k) so the boundary letter isn’t duplicated. Every row has the same length: E - A + 1 letters.