JavaScript Program to Print Character Pattern
Last Updated :
28 Jun, 2024
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:
Using Nested Loop
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.
JavaScript
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');
OutputA
AB
ABC
ABCD
ABCDE
Using Recursion
Recursively print a character pattern by appending a Character in each recursive call, incrementing the row number. The base case returns if the row exceeds the limit. Each call prints the pattern with an additional Character compared to the previous row.
Example: We will print a pattern of Character with increasing length per row, up to the specified number of rows (in this case, 5), using recursion.
JavaScript
function myPattern(rows, row = 1,
pattern = '') {
if (row > rows) return;
pattern += String.fromCharCode(64 + row) + ' ';
console.log(pattern);
myPattern(rows, row + 1, pattern);
}
myPattern(5);
OutputA
A B
A B C
A B C D
A B C D E
Using Array and Join
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.
JavaScript
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);
OutputA
BC
DEF
GHIJ
KLMNO
Using String Repeat Method
Using the string repeat method, generate character patterns efficiently by repeating a specified character for each row. This method calculates the number of characters needed per row and prints the pattern in a loop.
Example : In this example The repeatPattern function logs increasing repetitions of a character char up to n times
JavaScript
function repeatPattern(n, char) {
for (let i = 1; i <= n; i++) {
console.log(char.repeat(i));
}
}
repeatPattern(5, 'A');
OutputA
AA
AAA
AAAA
AAAAA
Similar Reads
JavaScript Program to Print Continuous Character Pattern A program to print a continuous character pattern generates a series of characters in a structured arrangement, typically forming shapes or designs. This pattern may involve repetition, alternation, or progression of characters, creating visually appealing outputs. The program iteratively prints cha
3 min read
C++ Program To Print Character Pattern Here we will build a C++ Program To Print Character patterns using 2 Approaches i.e. Using for loopUsing while loop Printing 1 character pattern in C++ using different approaches. 1. Using for loop Input: rows = 5 Output: A B B C C C D D D D E E E E E Approach 1: Assign any character to one variable
5 min read
C++ Program To Print Continuous Character Pattern Here we will build a C++ Program To Print Continuous Character patterns using 2 different methods i.e: Using for loopsUsing while loopsInput: rows = 5Output: A B C D E F G H I J K L M N O 1. Using for loopApproach 1: Assign any character to one variable for the printing pattern. The first for loop i
5 min read
C Program To Print Character Pyramid Pattern Pyramid patterns is a classic logical programming exercise where a triangular looking pattern is printed by treating the output screen as a matrix and printing a given character. In this article, we will explore how to print various alphabet pyramid patterns using C program.Half Pyramid PatternHalf
4 min read
C Program To Print Continuous Character Pyramid Pattern There are 2 ways to print continuous character patterns in C i.e: Using for loopUsing while loop Input: rows = 5 Output: A B C D E F G H I J K L M N O1. Using for loopApproach 1: Using CharacterAssign any character to one variable for the printing pattern. The first for loop is used to iterate the n
5 min read
Java Program to Print Alphabet Inverted Heart Pattern Alphabet Inverted Heart Pattern consists of two parts. The upper part of the pattern is a triangle. The base of the pattern has two peaks and a gap between them. Hence, the desired pattern looks like as shown in the illustration. Illustration: A BBB CCCCC DDDDDDD EEEEEEEEE FFFFFFFFFFF GGGGGGGGGGGGG
4 min read
Java Pattern Programs - Learn How to Print Pattern in Java In many Java interviews Star, number, and character patterns are the most asked Java Pattern Programs to check your logical and coding skills. Pattern programs in Java help you to sharpen your looping concepts (especially for loop) and problem-solving skills in Java. If you are looking for a place t
15+ min read
C Program To Print Diamond Pattern The Diamond Pattern is a symmetrical shape where the number of characters increases up to the centre and then decreases forming a diamond-like structure. It can be visualized as a full pyramid and an inverted full pyramid joined by their bases. In this article, we will learn how to print the Diamond
4 min read
Java Program to Print Diamond Shape Star Pattern In this article, we are going to learn how to print diamond shape star patterns in Java. Illustration: Input: number = 7 Output: * *** ***** ******* ********* *********** ************* *********** ********* ******* ***** *** * Methods: When it comes to pattern printing we do opt for standard ways of
6 min read
Java Program to Print Square Star Pattern Here, we will implement a Java program to print the square star pattern. We will print the square star pattern with diagonals and without diagonals. Example: ********************** * * * * * * * * * * * * ********************** Approach: Step 1: Input number of rows and columns. Step 2: For rows of
4 min read