How to use Nested Loop In Javascript
Using nested loops, iterate over rows and columns, incrementally printing based on row number. The outer loop controls rows, inner loop controls columns, printing Character for each column up to the row number.
Example: The function myPatternFunction iterates over rows, incrementally printing Characters based on row number, achieving a pattern with increasing lengths per row.
function myPattern(rows, startCharacter) {
const startCharCode = startCharacter.charCodeAt(0);
for (let i = 0; i < rows; i++) {
let pattern = '';
for (let j = 0; j <= i; j++) {
pattern += String
.fromCharCode(startCharCode + j);
}
console.log(pattern);
}
}
myPattern(5, 'A');
Output
A AB ABC ABCD ABCDE
JavaScript Program to Print Character Pattern
Printing a character pattern involves generating and displaying a structured arrangement of characters, often in a specific geometric shape such as a triangle or square. This process typically includes using loops to systematically place characters in rows and columns based on given rules.
Here are some common approaches:
Table of Content
- Using Nested Loop
- Using Recursion
- Using Array and Join