Open In App

JavaScript Program to Print Continuous Character Pattern

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 characters according to predefined rules or algorithms, often relying on loops and conditional statements to control the pattern’s structure and appearance.

Approach 1: Functional Approach

In the functional approach, a pattern row generator function creates each row based on the row number. Using ASCII encoding, adding 64 to a number converts it to the corresponding uppercase letter character (A-Z). Another function utilizes this generator to create and print each row using functional programming constructs like map and forEach.

Example: In this example The myFunction generates a string by repeating a character based on the row number. myResult uses myFunction to create an array of patterns for each row number, then prints each pattern. This approach streamlines pattern generation and printing for a specified number of rows.

Javascript
function myFunction(rowNumber) {
    return Array(rowNumber)
        .fill(String.fromCharCode(64 + rowNumber)).join(' ');
}

function myResult(rows) {
    Array.from({ length: rows }, (_, i) => i + 1)
        .map(myFunction)
        .forEach(row => console.log(row));
}

myResult(5);

Output
A
B B
C C C
D D D D
E E E E E

Approach 2: Using Nested Loop

The nested loop approach employs two loops: an outer loop controls the rows, and an inner loop controls the characters within each row. here we are Using ASCII encoding, adding 64 to a number converts it to the corresponding uppercase letter character (A-Z). The characters are determined based on the current row number, forming a continuous character pattern with increasing characters in each subsequent row.

Example: In this example Nested loops involve utilizing one loop inside another. In the given function, an outer loop controls the rows, while an inner loop generates characters based on the row number. This nested structure enables repetitive tasks, facilitating the creation of patterns or iteration over multi-dimensional data structures efficiently.

The function iterates through each row, incrementing characters based on the row number. It prints the pattern with characters separated by spaces, forming a continuous character pattern of increasing characters per row.

Javascript
function myFunction(rows) {
    for (let i = 1; i <= rows; i++) {
        let pattern = '';
        for (let j = 1; j <= i; j++) {
            pattern += String.fromCharCode(64 + i) + ' ';
        }
        console.log(pattern);
    }
}


myFunction(5);

Output
A 
B B 
C C C 
D D D D 
E E E E E 

Approach 3: Array.fill() and Array.join()

In this approach, an array of a specified length is filled with a given character using fill(), then converted to a string using join(). This string is printed repeatedly to form the desired pattern.

Example: In this example we The function myFunction prints a pyramid pattern of alphabets. It iterates through rows, constructing patterns of increasing alphabets based on row index and logs each pattern.

JavaScript
function myFunction(rows) {
    Array.from({ length: rows }, (_, i) => {
        const patternArray = new Array(i + 1).fill(String.fromCharCode(
        64 + i + 1) + ' ');
        const pattern = patternArray.join('');
        console.log(pattern);
    });
}

myFunction(5);

Output
A 
B B 
C C C 
D D D D 
E E E E E 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads