How to use Array and Join In Javascript
This approach builds a character pattern using nested loops and arrays. Each line is created as an array of characters, joined into a string, and stored in another array. The array of lines is then printed. Character codes are cycled from ‘A’ to ‘Z’.
Example : In this example the printPattern3 function generates a pattern of increasing lines of alphabetic characters, resetting from ‘Z’ to ‘A’ if needed. It prints each line, forming a triangular pattern of letters for `n` lines.
function printPattern3(n) {
let charCode = 65;
let arr = [];
for (let i = 0; i < n; i++) {
let line = [];
for (let j = 0; j <= i; j++) {
line.push(String.fromCharCode(charCode++));
if (charCode > 90) charCode = 65;
}
arr.push(line.join(''));
}
arr.forEach(line => console.log(line));
}
printPattern3(5);
Output
A BC DEF GHIJ KLMNO
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