Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

JavaScript Number Pattern 42

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

Photo Credit to CodeToFun

JavaScript Number Pattern 42

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

example.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html>
<body>

<script>
var i, j, k, m = 1;
for(i=1; i<=5; i++)
{
 for(j=1; j<=5; j++)
 {
  if(i == 1 || i == 5 || j == 1 || j == 5)
   document.write("1 ");
  else
   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:

Output
1 1 1 1 1 
1       1 
1       1 
1       1 
1 1 1 1 1

🧠 How the Program Works

Let's break down the logic behind the code:

  1. var i, j, k, m = 1;: This line declares and initializes four variables: i, j, k, and m. Among them, only m is initialized to the value 1. The other variables, i, j, and k, are declared but not immediately assigned any values.
  2. for(i=1; i<=5; i++): This is the outer loop that iterates over the values of i from 1 to 5 (inclusive). The loop starts with i set to 1 and increments i by 1 in each iteration.
  3. The opening curly brace { signifies the beginning of the code block associated with the outer loop.
  4. for(j=1; j<=5; j++): This is the inner loop that iterates over the values of j from 1 to 5 (inclusive). Similar to the outer loop, the inner loop starts with j set to 1 and increments j by 1 in each iteration.
  5. The opening curly brace { signifies the beginning of the code block associated with the inner loop.
  6. if(i == 1 || i == 5 || j == 1 || j == 5): This is a conditional statement that checks whether any of the following conditions are true:
    • i is equal to 1
    • i is equal to 5
    • j is equal to 1
    • j is equal to 5
  7. If any of the conditions mentioned above evaluates to true, the code inside this block is executed.
  8. Inside the conditional block, document.write("1 "); is used to write the string "1 " to the document. This will print a "1" followed by a space.
  9. If none of the conditions in the if statement are met, the else block is executed.
  10. Inside the else block, document.write("&nbsp;&nbsp; "); is used to write the HTML entity &nbsp; (which represents a non-breaking space) twice. This effectively prints two spaces to the document.
  11. The closing curly brace } indicates the end of the inner loop's code block.
  12. document.write("<br>");: This line writes an HTML line break (<br>) to the document, creating a new line after each iteration of the inner loop. This results in the next iteration of the outer loop starting on a new line.
  13. The closing curly brace } indicates the end of the outer loop's code block.

💯 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 42) 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