
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Number pattern in JavaScript
We are required to write a JavaScript and HTML program that provides the user with a text input and button. When the user enters any value in the input, say 5, and clicks the button, we should print the following pattern on the screen.
(for n = 5)
01 01 02 01 02 03 01 02 03 04 01 02 03 04 05
Example
The code for this will be −
<html> <head> <title>JavaScript Number Patterns</title> <script type="text/javascript"> const printPattern = () => { const num = document.getElementById("rows").value; for(let m=1; m <= num; m++){ for(let n=1; n <= m; n++){ document.write("0"+n+" "); } document.write("<br />"); } } </script> </head> <body> <p>Enter the number of rows and press print</p> <input type="number" placeholder="Number of Rows" id="rows"> <button type="button" onclick="printPattern()">Print</button> </body> </html>
Output
And the output in the console will be −
Before entering values −
Final result −
- Related Articles
- Zig-Zag pattern in strings in JavaScript?
- Program to print number pattern in C
- The algorithm problem - Backtracing pattern in JavaScript
- How to validate a date pattern in JavaScript?
- How to understand JavaScript module pattern?
- PHP program to print the number pattern
- How to search a string for a pattern in JavaScript?
- How to print star pattern in JavaScript in a very simple manner?
- Pattern pattern() method in Java with examples
- Check if a number has bits in alternate pattern - Set 1 in C++
- How to create a chessboard pattern with JavaScript and DOM?
- Finding Gapful number in JavaScript
- Reverse a number in JavaScript
- Factorize a number in JavaScript
- Number to alphabets in JavaScript

Advertisements