Pyramid in JavaScript - JavaScript Tutorial

Pyramid in JavaScript

BLUF: This tutorial on Pyramid in JavaScript provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: Pyramid in JavaScript

Understanding Pyramid in JavaScript is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

In this article, we will explore the concept of the pyramid in JavaScript. Before diving deeper, let's first grasp what a pyramid entails.

What is a pyramid?

In geometric terms, a pyramid refers to a triangular formation; however, within the context of JavaScript, a pyramid denotes a specific arrangement or sequence of numbers that is organized in a triangular shape.

There are numerous methods to construct a pyramid using stars (*) or numbers.

Illustrations of constructing a pyramid using JavaScript

Let us explore the process of creating a pyramid in JavaScript through the use of examples.

Demo 1:

In the following demonstration, we will create a pyramid pattern using asterisks (*) by employing both a for loop and nested for loops.

Code:

Example

let n = 6;

for (let p = 1; p <= n; p++) {
  for (let q = 1; q <= n - p; q++) {
    process.stdout.write(' ')
  }
  
  for (let m = 0; m < 2 * p - 1; m++) {
    process.stdout.write('*')
  }
  
  console.log();
}

Explanation:

  • First of all, we have constructed a variable n and assigned it an integer value. This value represents a total number of rows.
  • We have constructed for loop which is utilized to iterate the total number of rows.
  • Then we have constructed a nested for loop that prints space n-p times. If p is 4 then it will print 3 spaces.
  • Then we have constructed another nested loop which is utilized to print stars (2 * p - 1) times. If value of p is 2 then it will print 3 stars.

Output:

In this output, we can observe that the star pattern pyramid is formed through the utilization of three nested for loops.

Demo 2:

We are going to create an upper pyramid star design by employing just one for loop.

Code:

Example

function createPyramid(rows) {
    let pyramid = '';
    for (let k = 0; k < rows; k++) {
        pyramid += ' '.repeat(rows - k - 1);
        pyramid += '*'.repeat(2 * k + 1);
        pyramid += '\n';
    }
    console.log(pyramid);
}

createPyramid(6);

Explanation:

  • First of all, we have created a function called "createPyramid" which has a parameter called "rows".
  • Then under that function, we have declared a variable called "pyramid".
  • We have then constructed for loop to iterate each row.
  • for loop is utilized to first add spaces then adds star (*) and then breaks the line and moves to the next line.

Output:

In the following output, it can be observed that the pyramid star pattern is generated using a single loop.

Demo 3:

We will create a pattern of a horizontally inverted pyramid as demonstrated below, oriented downwards.

Code:

Example

let n = 5; 
for (let k = n; k >= 1; k--) { 
	let str = "* "; 
	let space = '  '; 
	console.log(space.repeat((n - k)) + str.repeat(k * 2 - 1)); 
}

Explanation:

  • We have first of all declared a variable "n" and assigned it a value that acts as a number of rows.
  • Then we have created for loop to iterate each row.
  • The for loop first adds a star (*) then adds spaces and prints each row of the pyramid.

Output:

In the output below, we can observe the stars arranged in the form of a pyramid.

Demo 4:

In this demonstration, we will create the correct inverted pyramid pattern.

Code:

Example

let n = 6; 
for (let k = 1; k <= n; k++) { 
	let str = "* "; 
	console.log(str.repeat(k)); 
} 
for (let k = n - 1; k >= 1; k--) { 
	let str = "* "; 
	console.log(str.repeat(k)); 
}

Explanation:

  • We have first of all declared a variable "n" and assigned it a value that represents the number of rows.
  • Then we have added a for loop to iterate each row which prints a row of stars increasing the number of rows in each row by 1.
  • Then we have then added for loop to iterate each row which prints a row of stars decreasing the number of rows in each row by 1.

Output:

The output displays a clear example of the correct inverted pyramid structure.

Demo 5:

We will build the left inverted pyramid pattern.

Code:

Example

let n = 5; 
for (let k = 1; k <= n; k++) { 
	let str = "* "; 
	let space = '  '; 
	console.log(space.repeat((n - k)) + str.repeat(k)); 
} 

for (let k = n - 1; k >= 1; k--) { 
	let str = "* "; 
	let space = '  '; 
	console.log(space.repeat(n - k) + str.repeat(k)); 
}

Explanation:

  • We have first of all declared a variable "n" and initialized its value.
  • We have then constructed a for loop which adds a decreasing number of spaces and repeats the stars to form a pattern. This for loop creates half part of the pyramid.
  • We have then constructed another for loop which adds an increasing number of spaces and repeats the stars to form a pattern. This for loop creates another part of the pyramid.

Output:

The output distinctly displays the pattern of a left inverted pyramid.

Demo 6:

We are going to develop a program that generates a numerical pattern resembling a pyramid.

Code:

Example

let n = 5;
for (let p = 1; p <= n; p++) {
	let str = '';
	let count = 1;
	for (let q = 1; q <= 2 * n; ++q) {
		if (p + q >= n + 1 && (p >= q - n + 1)) {
			str += count.toString() + ' ';
			count++;
		} else {
			str += '  ';
		}
	}

	console.log(str);
}

Explanation:

  • We have first declared a variable "n" and allotted it a value that acts as the number of rows.
  • Then we have constructed for loop which is the outer loop that controls the rows of the pyramid and generates n number of rows.
  • After that, we have created a nested for loop that controls the columns of the pyramid.
  • We have used console.log(str) to print the output.

Output:

Below is the output arranged in a downward manner, showcasing the pyramid structure of digits.

Demo 7:

We will create a pattern of numbers in the shape of an inverted pyramid.

Code:

Example

let n = 6;
for (let g = n; g >= 1; g--) {
	let str = '';
	let count = 1;
	for (let h = 1; h <= 2 * n; ++h) {
		if (g + h >= n + 1 && (g >= h - n + 1)) {
			str += count.toString() + ' ';
			count++;
		} else {
			str += '  ';
		}
	}

	console.log(str);
};

Explanation:

  • First of all, we have declared a variable "n" and allotted it a value which will be the number of rows.
  • Then we have constructed a for loop which is the outer loop that controls the rows of the pyramid.
  • After that, we have created an inner for loop that controls the columns of the pyramid.
  • We have used console.log(str) to print the resultant pyramid.

Output:

Below is the output arranged in a downward fashion, showcasing the inverted pyramid configuration of numbers.

Demo 8:

We will create a sequence of numbers that extends towards the left.

Code:

Example

let n = 6;
for (let g = 1; g <= n; g++) {
	let str = '';
	let count = 1;
	for (let h = 1; h <= n; ++h) {
		if (g + h >= 1 + n && h <= n) {
			str += count.toString() + ' ';
			count += 1;
		} else {
			str += '  ';
		}
	}

	console.log(str);
};
for (let g = n - 1; g >= 1; g--) {
	let str = '';
	let count = 1;
	for (let h = 1; h <= n; ++h) {
		if (g + h >= 1 + n && h <= n) {
			str += count.toString() + ' ';
			count += 1;
		} else {
			str += '  ';
		}
	}
	console.log(str);
};

Output:

The output distinctly displays a pyramid arrangement of numbers directed towards the left as it descends.

Demo 9:

We are going to create a sequence of numbers in the correct orientation.

Code:

Example

let n = 5;
for (let g = 1; g <= n; g++) {
	let str = "";
	let count = 1;
	for (let k = 1; k <= g; k++) {
		str = count.toString() + " " + str;
		count += 1;
	}
	for (let h = g; h < n; h++) {
		str += " ";
	}
	console.log(str);
}
for (let g = n - 1; g >= 1; g--) {
	let str = "";
	let count = 1;
	for (let k = 1; k <= g; k++) {
		str = count.toString() + " " + str;
		count += 1;
	}
	for (let h = g; h < n; h++) {
		str += " ";
	}
	console.log(str);
};

Output:

The output presented here distinctly showcases the pyramid arrangement of numbers oriented towards the right.

Demo 10:

We will create a numerical pattern in the form of an inverted half pyramid.

Code:

Example

function createInvertedHalfPyramid(rows) {
	for (let p = rows; p >= 1; p--) {
		let row = '';
		for (let q = 1; q <= p; q++) {
			row += q + ' ';
		}
		console.log(row);
	}
}

createInvertedHalfPyramid(6);

Output:

Below, we can observe the downward arrangement that showcases the inverted half pyramid structure formed by numbers.

Conclusion:

In this article, we have explored the concept of a pyramid in JavaScript. We have examined different methods to create both a numerical pyramid and a star pyramid through a series of examples.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience