JavaScript Star Pattern 11
Photo Credit to CodeToFun
JavaScript Star Pattern 11
Here`s a program that prints the above star pattern using JavaScript Programming:
<!DOCTYPE html>
<html>
<body>
<script>
var i, j, k;
// First Part
for(i=1; i<=5; i++)
{
for(j=i; j<=5; j++)
document.write("*");
for(k=1; k<(i*2)-1; k++)
document.write("&nbsp;&nbsp;");
for(j=i; j<=5; j++)
document.write("*");
for(k=1; k<i*2; k++)
document.write("&nbsp;&nbsp;");
document.write("<br>");
}
// Second Part
for(i=4; i>=1; i--)
{
for(j=5; j>=i; j--)
document.write("*");
for(k=1; k<(i*2)-1; k++)
document.write("&nbsp;&nbsp;");
for(j=5; j>=i; j--)
document.write("*");
for(k=1; k<i*2; k++)
document.write("&nbsp;&nbsp;");
document.write("<br>");
}
</script>
</body>
</html>
💻 Testing the Program
When you run the above program, it will print the following output:
********** **** **** *** *** ** ** * * ** ** *** *** **** **** **********
🧠 How the Program Works
Let's break down the logic behind the code:
- Initialize variables:
- The variables i, j, and k are declared at the beginning of the script. These variables are used as loop counters to control the iteration through the different parts of the diamond pattern.
- First Part - Top Half of the Diamond:
- The outermost loop for(i=1; i<=5; i++) iterates from i=1 to i=5, controlling the number of rows in the top half of the diamond.
- The first inner loop for(j=i; j<=5; j++) prints asterisks (*) for each column in the row, from j=i to j=5.
- The second inner loop for(k=1; k<(i*2)-1; k++) prints two spaces (&nbsp;&nbsp;) for each column in the row, except for the first column. The number of spaces decreases as i increases.
- The third inner loop for(j=i; j<=5; j++) prints another set of asterisks (*) for each column in the row, similar to the first loop.
- The fourth inner loop for(k=1; k<i*2; k++) prints two spaces (&nbsp;&nbsp;) for each column in the row, to create spacing between the two sets of asterisks.
- document.write("<br>"); is used to move to the next line after each row is completed, creating the vertical structure of the diamond.
- Second Part - Bottom Half of the Diamond:
- The second part mirrors the first part's pattern, but in reverse order to create the bottom half of the diamond.
- The outer loop for(i=4; i>=1; i--) iterates from i=4 to i=1, controlling the number of rows in the bottom half of the diamond.
- The rest of the loops in this section follow the same logic as in the first part but in reverse order, with the loops responsible for printing asterisks and spaces switched around.
- Output:
- The combination of the first and second parts creates a diamond pattern with asterisks and spaces.
💯 Tips for Enhancement:
Explore the versatility of this pattern by adjusting its parameters. Whether you increase or decrease the size, tweak the spacing, or modify the characters used, each change opens up a world of possibilities, allowing you to customize and create your unique visual effects.
✔ Conclusion:
Creating visually appealing patterns is not only a fun endeavour but also a great way to enhance your programming or design skills. We hope this tutorial has inspired you to explore the world of creative coding. Share your creations with us, and let your imagination run wild!
🤗 Closing Call-to-Action:
We'd love to see your unique interpretations of the star pattern. Share your creations in the comments below, and don't hesitate to reach out if you have any questions or suggestions for future tutorials. Happy coding!
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (JavaScript Star Pattern 11) please comment here. I will help you immediately.