Star pattern program in JavaScript
In this post, you will learn JavaScript code to print different star patterns. Such type of logical question is generally asked in interviews or in competitive examinations. This can be helpful to improve your logical programming skills and is helpful in game development and other logical application development.
JavaScript Program to Print Pyramid Star Pattern
The following JavaScript program prints a pyramid pattern made up of stars up to the specified 6 lines. Here, the rows demonstrate a number of lines that will be printed in the pyramid shape. In this pattern program, we have used nested for loops. The first inner for loop prints the spaces, and the second inner loop prints the stars.
let rows = 6;
let string = "";
// External loop
for (let i = 1; i <= rows; i++) {
// printing spaces
for (let j = 1; j <= rows - i; j++) {
string += " ";
}
// printing star
for (let k = 0; k < 2 * i - 1; k++) {
string += "*";
}
string += "\n";
}
console.log(string);
Output of the above code:
*
***
*****
*******
*********
***********
Reversed pyramid star pattern in JavaScript
The following JavaScript program prints a reverse pyramid pattern made up of stars up to the specified n lines. In this program, we take a row number as input from the user. Here, the row demonstrates a number of lines that will be printed in the reverse triangle shape.
let rows = 6;
let string = "";
// External loop
for (let i = 0; i < rows; i++) {
// printing spaces
for (let j = 0; j < i; j++) {
string += " ";
}
// printing star
for (let k = 0; k < 2 * (rows-i) - 1; k++) {
string += "*";
}
string += "\n";
}
console.log(string);
Output of the above code:
***********
*********
*******
*****
***
*
JavaScript Program to Print Diamond Shape Star Pattern
The given JavaScript diamond star pattern program example uses two sets of loops to print the upper and lower portions of a diamond. It iterates from 1 to rows and back to 1 to cover the diamond structure for rows. The nested loops are used to iterate rows and columns and print the diamond star pattern.
let rows = 6;
let string = "";
// Upper triangle shape
for (let i = 1; i <= rows; i++) {
// printing spaces
for (let j = rows; j > i; j--) {
string += " ";
}
// printing star
for (let k = 0; k < i * 2 - 1; k++) {
string += "*";
}
string += "\n";
}
// Lower triangle shape
for (let i = 1; i <= rows - 1; i++) {
// printing spaces
for (let j = 0; j < i; j++) {
string += " ";
}
// printing star
for (let k = (rows - i) * 2 - 1; k > 0; k--) {
string += "*";
}
string += "\n";
}
console.log(string);
Output of the above code:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Hollow diamond pattern in JavaScript
The following JavaScript program prints Hollow Diamond Pattern made up of stars up to the specified rows. Here, the rows variable demonstrates the number of lines that will be printed in one triangle shape of the Hollow Diamond design. Suppose, we get an input row value of 6, then the total number of lines in the Hollow diamond pattern will be 11. The given program uses nested for loops and if-else statements to return the hollow diamond pattern.
let rows = 6;
let char = "";
// Upper triangle shape
for (let i = 1; i <= rows; i++) {
// printing spaces
for (let j = rows; j > i; j--) {
char += " ";
}
// printing star
for (let k = 0; k < i * 2 - 1; k++) {
if (k === 0 || k === 2 * i - 2) {
char += "*";
}
else {
char += " ";
}
}
char += "\n";
}
// Lower triangle shape
for (let i = 1; i <= rows - 1; i++) {
// printing spaces
for (let j = 0; j < i; j++) {
char += " ";
}
// printing star
for (let k = (rows - i) * 2 - 1; k >= 1; k--) {
if (k === 1 || k === (rows - i) * 2 - 1) {
char += "*";
}
else {
char += " ";
}
}
char += "\n";
}
console.log(char);
Output of the above code:
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
Right pascal star pattern in JavaScript
The right pascal star pattern is the right triangle star pattern and the reversed triangle star pattern together. In the given JavaScript program, we have used nested loops to print the right pascal star pattern.
let rows = 6;
let char = "";
for (let i = 1; i <= rows; i++) {
for (let j = 0; j < i; j++) {
char += "*";
}
char += "\n";
}
for (let i = 1; i <= rows - 1; i++) {
for (let j = 0; j < rows - i; j++) {
char += "*";
}
char += "\n";
}
console.log(char);
*
**
***
****
*****
******
*****
****
***
**
*
Left pascal star pattern in JavaScript
The left pascal star pattern is the left triangle star pattern and the reversed triangle star pattern together. In the given JavaScript program, we have used nested for loops to print the left pascal star pattern.
let rows = 6;
let char = "";
for (let i = 1; i <= rows; i++) {
for (let j = 0; j < rows - i; j++) {
char += " ";
}
for (let k = 0; k < i; k++) {
char += "*";
}
char += "\n";
}
for (let i = 1; i <= rows - 1; i++) {
for (let j = 0; j < i; j++) {
char += " ";
}
for (let k = 0; k < rows - i; k++) {
char += "*";
}
char += "\n";
}
console.log(char);
Output of the above code:
*
**
***
****
*****
******
*****
****
***
**
*
Downward triangle star pattern in JavaScript
The given JavaScript program to print the downward triangle star pattern uses a nested for loop. It is also known as the reverse star pattern.
let rows = 6;
let char = "";
for (let i = 0; i < rows; i++) {
// printing star
for (let k = 0; k < rows - i; k++) {
char += "*";
}
char += "\n";
}
console.log(char);
Output of the above code:
******
*****
****
***
**
*
Left triangle star pattern in JavaScript
The given JavaScript program generates the left triangle star by nesting two for loops, with the external loop handling pattern columns and the internal loop handling pattern rows.
let rows = 6;
let char = "";
for (let i = 1; i <= rows; i++) {
for (let j = 0; j < i; j++) {
char += "*";
}
char += "\n";
}
console.log(char);
Output of the above code:
*
**
***
****
*****
******
Right triangle star pattern in JavaScript
The given JavaScript program generates the right triangle star by nesting two for loops, with the external loop handling pattern columns and the internal loop handling pattern rows.
let rows = 6;
let char = "";
for (let i = 1; i <= rows; i++) {
// printing spaces
for (let j = 0; j < rows - i; j++) {
char += " ";
}
// printing star
for (let k = 0; k < i; k++) {
char += "*";
}
char += "\n";
}
console.log(char);
Output of the above code:
*
**
***
****
*****
******
Related Articles
Palindrome number in JavaScriptSimple calculator using JavaScript
How to reverse string in Javascript
JavaScript display PDF in the browser using Ajax call
Parsing JSON in Javascript
Javascript speech recognition example
Select/deselect all checkboxes using Javascript
Print specific part of a web page in javascript
Dynamically Add/Delete HTML Table Rows Using Javascript
jQuery Ajax serialize form data example
Image popup on page load using HTML and jQuery
Ajax live data search using jQuery PHP MySQL
jQuery loop over JSON result after AJAX Success
Simple star rating system using PHP, jQuery and Ajax
jquery image zoom on mouseover example
Bootstrap star rating input example
Bootstrap datepicker example
Submit a form data without page refresh