Diagonal Star in a Letter Grid

What You'll Learn
Think of each row as five columns labeled by letter codes j = 69, 68, …, 65 (E on the left through A on the right). The row index i also walks A to E. Whenever i and j are equal, that cell shows *; every other cell shows the letter for j. As i increases, the star shifts one column left each row (from the A side toward the E side).
The original snippet declared var k = 65 but never used it; clean code only needs i and j.
⭐ Pattern Output
EDCB*
EDC*A
ED*BA
E*CBA
*DCBANode.js / console version
Build each row as a string; use strict equality === for the diagonal check:
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
for (let i = A; i <= top; i++) {
let line = "";
for (let j = top; j >= A; j--) {
line += i === j ? "*" : String.fromCharCode(j);
}
console.log(line);
}Browser version (document.write)
Same logic as the reference; no unused variables:
<!DOCTYPE html>
<html>
<body>
<script>
const A = 65;
const top = 69;
for (let i = A; i <= top; i++) {
for (let j = top; j >= A; j--) {
if (i === j) document.write("*");
else document.write(String.fromCharCode(j));
}
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Outer loop: i from A to E
Each row corresponds to one “row letter” code, stepping by 1.
Inner loop: j from E to A
Every row prints five characters, left to right in descending letter order.
i === j
Diagonal in code space: replace that letter with *. Otherwise output fromCharCode(j).
Moving star
The * appears at column A on the first row and reaches column E on the last.
💡 Tips for Enhancement
Try These
- Swap
*for another symbol or use a space for the diagonal - Generalize
topto any uppercase letter and keepi, jin range - Compare with Program 8 (reverse segments without a full grid)
Avoid
- Using
==when you intend type-safe code (prefer===for codes) - Copying unused
kfrom older examples
Key Takeaways
Full E…A scan on every row with one * where i === j.
Exactly 5 stars total (one per row) for the A–E square.
Letter cells still use j so the grid stays readable off the diagonal.
Complexity: O(n²) for n letters (n = 5 here).
❓ Frequently Asked Questions
i = 65 (A), the only time j equals 65 is the last inner iteration, so the star sits in the rightmost column.i === j is the main diagonal; printing j off-diagonal fills the rest of the row with letters.n rows and n columns.Next: JavaScript Alphabet Pattern 18
Palindrome alphabet pyramid: A, ABA, ABCBA, …
If you read only the letters (skip the stars), each row is still the full reverse alphabet slice E through A with one gap — the gap shifts one step left each row.
10 people found this page helpful
