Here’s a Node.js program that takes user input to print a star pattern. We will prompt the user to enter the number of rows they want in the pattern and then generate the pattern accordingly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); function printPattern(rows) { for (let i = 1; i <= rows; i++) { let pattern = ''; for (let j = 1; j <= i; j++) { pattern += '* '; } console.log(pattern); } } rl.question('Enter the number of rows for the star pattern: ', (rows) => { const numberOfRows = parseInt(rows); if (isNaN(numberOfRows) || numberOfRows <= 0) { console.log('Invalid input. Please enter a positive number.'); } else { printPattern(numberOfRows); } rl.close(); }); |
When you run this program, it will prompt you to enter the number of rows for the star pattern. After you enter a valid positive number, it will generate and display the pattern accordingly.
See the below screenshot: