Diamond With Stars

What You'll Learn
This pattern prints a symmetric “diamond” using letters and stars. Each row uses the same letter (A, then B, then C, …) and inserts * between letters by checking whether the position is even or odd.
⭐ Pattern Output
A
B*B
C*C*C
D*D*D*D
E*E*E*E*E
D*D*D*D
C*C*C
B*B
ANode.js / console version
Print the top half (1..5), then the bottom half (4..1). Odd positions print the row letter; even positions print *.
const top = 5;
// Top half
for (let i = 1; i <= top; i++) {
let line = "";
for (let j = 1; j < i * 2; j++) {
line += j % 2 === 0 ? "*" : String.fromCharCode(i + 64);
}
console.log(line);
}
// Bottom half
for (let i = top - 1; i >= 1; i--) {
let line = "";
for (let j = 1; j < i * 2; j++) {
line += j % 2 === 0 ? "*" : String.fromCharCode(i + 64);
}
console.log(line);
}Browser version (document.write)
Same logic as the reference, using document.write and line breaks.
<!DOCTYPE html>
<html>
<body>
<script>
// Top half
for (let i = 1; i <= 5; i++) {
for (let j = 1; j < i * 2; j++) {
if (j % 2 === 0) document.write("*");
else document.write(String.fromCharCode(i + 64));
}
document.write("<br>");
}
// Bottom half
for (let i = 4; i >= 1; i--) {
for (let j = 1; j < i * 2; j++) {
if (j % 2 === 0) document.write("*");
else document.write(String.fromCharCode(i + 64));
}
document.write("<br>");
}
</script>
</body>
</html>🧠 How It Works
Top half: i from 1 to top
Row i prints 2i - 1 characters.
Inner loop: j from 1 to 2i - 1
Odd positions output the row letter (String.fromCharCode(i + 64)), even positions output *.
Bottom half: i from top - 1 down to 1
Repeat the same alternating logic while shrinking the width to mirror the top half.
Letter-star diamond
Two halves create a symmetric shape with * between repeated row letters.
Key Takeaways
Each row prints 2i - 1 characters.
Odd positions print letters; even positions print *.
Two halves (up + down) create the diamond.
Time complexity: O(n²).
❓ Frequently Asked Questions
"*" literal with "-" (or any character/string) in the even-position branch.top (and the bottom loop start to top - 1). The letter is computed from i via String.fromCharCode(i + 64).Keep going
Browse the full list of alphabet patterns for more loop practice.
String.fromCharCode(i + 64) maps 1→A, 2→B, 3→C, … (uppercase ASCII).
10 people found this page helpful
