Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

JavaScript Number Pattern 35

Updated on Jan 10, 2024
By Mari Selvan
👁️ 202 - Views
⏳ 4 mins
💬 1 Comment
JavaScript Number Pattern 35

Photo Credit to CodeToFun

JavaScript Number Pattern 35

Here`s a program that prints the above number pattern using JavaScript Programming:

example.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html>
 <head>
  <style>
   div{
    display: inline-block;
    width: 20px;
    text-align: center;
   }
  </style>
 </head>
<body>

<script>
var i, j, k = 1;
for(i=1; i<=5; i++)
{
 for(j=5; j>=1; j--)
 {
  if(j > i)
   document.write("<div></div>");
  else
   document.write("<div>"+ k++ +"</div>");
 }
 document.write("<br>");
}
</script>
</body>
</html>

💻 Testing the Program

When you run the above program, it will print the following output:

Output
             1
          2  3
       4  5  6
   7  8  9  10
11 12 13 14 15

🧠 How the Program Works

Let's break down the logic behind the code:

  1. Variable Initialization: The variables i, j, and k are declared and initialized with values 1.
  2. Outer Loop (i Loop): The outer loop runs from i = 1 to i <= 5. This loop controls the rows of the pattern.
  3. Inner Loop (j Loop): The inner loop runs from j = 5 to j >= 1. This loop controls the columns of the pattern.
  4. Conditional Statements: Inside the inner loop, there's a conditional statement:
    • If j is greater than i, it means we're in a position where we need to add an empty <div> element. So, document.write("<div></div>"); is executed.
    • If j is not greater than i, it means we're in a position where we need to add a numbered <div> element. The k value is concatenated to the inner HTML of the <div> element, and k is incremented.
  5. Line Break (<br>): After each inner loop iteration (after each row is complete), a <br> element is added to create a line break and move to the next row.
  6. Output Generation: The program uses document.write() to write the generated <div> elements and line breaks to the HTML document.
  7. Final Result: The program generates a pattern where in the first row, all five columns contain empty <div> elements. In the second row, the first column contains a numbered <div> element, and the remaining columns contain empty <div> elements. This pattern continues, with the number in the <div> element increasing in each row, until you reach the last row, where all five columns contain numbered <div> elements starting from 1 and increasing by 1 in each column.

💯 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 number 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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
1 year ago

If you have any doubts regarding this article (JavaScript Number Pattern 35) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy