Learn how to print the pyramid pattern in javascript.
There are three different types of pyramid pattern and we are going to see each of them.
Left Pyramid pattern
Example
'*' '**' '***' '****' '*****'
Implementation
- We will represent the pyramid pattern with
'*'
. - We are going to use two different loops to print the left pyramid pattern.
- For each row we will print the no of
'*'
as per its respective position.
let pyramidLeft = (rows) => { for(let i = 1; i <= rows; i++){ let str = ''; for(let j = 1; j <= i; j++){ //Count the '*' for each row str += '*'; } //Print the pyramid pattern for each row console.log(str); } };
Input: pyramidLeft(5); Output: "*" "**" "***" "****" "*****"
Time complexity: O(n ^ 2).
Space complexity: O(1).
Time and Space complexity
- We are printing the no of
'*'
for each respective rows like n * (n - 1) * (n - 2) ..., so Time complexity is O(n ^ 2). - We are using constant space, so Space complexity is O(1).
Right pyramid pattern
In order to print the reverse pyramid, we will need to add the white space to the left, so that we can shift the '*'
to the right.
Example
" *" " **" " ***" " ****" "*****"
Implementation
- We will use two inner loops to print the right pyramid pattern.
- The first loop will add the white space to the left.
- Second loop will add the
'*'
to the string.
let pyramidRight = (rows) => { for(let i = 1; i <= rows; i++){ let str = ''; //Add the white space to the left for(let k = 1; k <= rows - i; k++){ str += ' '; } //Add the '*' for each row for(let j = 1; j <= i; j++){ str += '*'; } //Print the pyramid pattern for each row console.log(str); } }
Input: pyramidRight(5); Output: " *" " **" " ***" " ****" "*****"
Time complexity: O(n ^ 2).
Space complexity: O(1).
Time and Space complexity
- We are using two different loops inside another loop, the first inner loops adds white space and second inner loop adds the
'*'
so they together run for n times and it happens for each row, so Time complexity is O(n * n) = O(n ^ 2). - We are using constant space for adding the white space and
'*'
, so Space complexity is O(1).
Complete pyramid pattern in javascript
Just like right, For complete pattern we will need to add the white on the left as well as on the right.
Example
" * " " *** " " ***** " " ******* " "*********"
Implementation
- We will use three different inner loops to print the complete pyramid pattern.
- First loop will add the white space on the left, then second loop will add the odd numbers
'*'
for each row, Last loop will add white space on the right.
let pyramidComplete = (rows) => { for(let i = 1; i <= rows; i++){ let str = ''; //Add the white space to the left for(let k = 1; k <= (rows - i); k++){ str += ' '; } //Add the '*' for each row for(let j = 0; j != (2 * i - 1); j++){ str += '*'; } //Add the white space to the right for(let k = i + 1; k <= rows; k++){ str += ' '; } //Print the pyramid pattern for each row console.log(str); } }
Input: pyramidComplete(5); Output: " * " " *** " " ***** " " ******* " "*********"
Time complexity: O(n ^ 2).
Space complexity: O(1).
Time and Space complexity
- We are using three different loops inside another loops, the first inner loop adds white space on left and second inner loop adds the
'*'
and the third loop adds the white space on right, so they together run for n times and it happens for each row, so Time complexity is O(n * n) = O(n ^ 2). - We are using constant space for adding the white space on the left and right and
'*'
, so Space complexity is O(1).