Diagonal Star in a Letter Grid

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
i === j*

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

Output
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA
1

Node.js / console version

Build each row as a string; use strict equality === for the diagonal check:

JavaScript
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);
}
2

Browser version (document.write)

Same logic as the reference; no unused variables:

HTML
<!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

1

Outer loop: i from A to E

Each row corresponds to one “row letter” code, stepping by 1.

5 rows
2

Inner loop: j from E to A

Every row prints five characters, left to right in descending letter order.

Columns
3

i === j

Diagonal in code space: replace that letter with *. Otherwise output fromCharCode(j).

Star band
=

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 top to any uppercase letter and keep i, j in range
  • Compare with Program 8 (reverse segments without a full grid)

Avoid

  • Using == when you intend type-safe code (prefer === for codes)
  • Copying unused k from older examples

Key Takeaways

1

Full E…A scan on every row with one * where i === j.

2

Exactly 5 stars total (one per row) for the A–E square.

3

Letter cells still use j so the grid stays readable off the diagonal.

4

Complexity: O(n²) for n letters (n = 5 here).

❓ Frequently Asked Questions

For i = 65 (A), the only time j equals 65 is the last inner iteration, so the star sits in the rightmost column.
Yes: if you index rows and columns by the same letter codes, the condition i === j is the main diagonal; printing j off-diagonal fills the rest of the row with letters.
5 × 5 = 25 character writes for this fixed pattern; in general O(n²) for n rows and n columns.

Next: JavaScript Alphabet Pattern 18

Palindrome alphabet pyramid: A, ABA, ABCBA, …

Program 18 →
Did you know?

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.

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.

10 people found this page helpful