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 −