Introduction to Star Patterns in JavaScript
Jul 25, 2024
Star patterns are a popular way to practice and improve your programming skills in JavaScript. By creating various shapes and designs using stars, you can enhance your understanding of loops, nested loops, and logic building. In this blog post, we will explore different types of star patterns, their approaches, and provide code snippets to help you create them.
Types of Star Patterns
Square Star Pattern
Hollow Square Star Pattern
Right Triangle Star Pattern
Left Triangle Star Pattern
Downward Triangle Star Pattern
Hollow Triangle Star Pattern
Pyramid Star Pattern
Reversed Pyramid Star Pattern
Hollow Pyramid Star Pattern
Diamond Star Pattern
Square Star Pattern
The square star pattern is the simplest form of a star pattern. It creates a square-shaped pattern of stars by using nested loops.Approach:
Run two nested loops, one for rows and one for columns.
The outer loop runs for
n
times, wheren
is the number of rows and columns in the square.The inner loop also runs for
n
times, printing a star for each iteration.After the inner loop completes, print a newline character to move to the next row.
Code:
Hollow Square Star Pattern
The hollow square star pattern creates a square with stars only on the edges, leaving the center empty.Approach:
Run two nested loops, one for rows and one for columns.
The outer loop runs for
n
times, wheren
is the number of rows and columns in the square.The inner loop runs for
n
times as well.If the current row is the first or last row, or the current column is the first or last column, print a star.
Otherwise, print a space.
After the inner loop completes, print a newline character to move to the next row.
Code:
Right Triangle Star Pattern
The right triangle star pattern creates a right-angled triangle using stars.Approach:
Run two nested loops, one for rows and one for columns.
The outer loop runs for
n
times, wheren
is the number of rows in the triangle.The inner loop runs for
i
times, wherei
is the current row number.In each iteration of the inner loop, print a star.
After the inner loop completes, print a newline character to move to the next row.
Code:
Left Triangle Star Pattern
The left triangle star pattern creates a left-aligned triangle using stars.Approach:
Run two nested loops, one for rows and one for columns.
The outer loop runs for
n
times, wheren
is the number of rows in the triangle.The inner loop runs for
n - i
times, wherei
is the current row number.In each iteration of the inner loop, print a space.
After the inner loop completes, run another loop for
i
times, printing a star in each iteration.After the second inner loop completes, print a newline character to move to the next row.
Code:
Downward Triangle Star Pattern
The downward triangle star pattern creates a downward-facing triangle using stars.Approach:
Run a single loop for
n
times, wheren
is the number of rows in the triangle.In each iteration, print
n - i + 1
stars, wherei
is the current row number.After each iteration, print a newline character to move to the next row.
Code:
Hollow Triangle Star Pattern
The hollow triangle star pattern creates a triangle with stars only on the edges, leaving the center empty.Approach:
Run two nested loops, one for rows and one for columns.
The outer loop runs for
n
times, wheren
is the number of rows in the triangle.The inner loop runs for
i
times, wherei
is the current row number.If the current row is the first row or the last row, or the current column is the first column or the last column for that row, print a star.
Otherwise, print a space.
After the inner loop completes, print a newline character to move to the next row.
Code:
Pyramid Star Pattern
The pyramid star pattern creates a pyramid-shaped pattern using stars.Approach:
Run two nested loops, one for rows and one for spaces.
The outer loop runs for
n
times, wheren
is the number of rows in the pyramid.The first inner loop runs for
n - i
times, printing spaces in each iteration.The second inner loop runs for
2 * i - 1
times, printing stars in each iteration.After the inner loops complete, print a newline character to move to the next row.
Code:
Hollow Pyramid Star Pattern
The hollow pyramid star pattern creates a pyramid-shaped pattern with stars only on the edges, leaving the center empty.Approach:
Run two nested loops, one for rows and one for spaces and stars.
The outer loop runs for
n
times, wheren
is the number of rows in the hollow pyramid.The first inner loop runs for
n - i
times, printing spaces in each iteration.The second inner loop runs for
2 * i - 1
times.If the current row is the first row or the last row, or the current column is the first column or the last column for that row, print a star.
Otherwise, print a space.
After the inner loops complete, print a newline character to move to the next row.
Code:
Diamond Star Pattern
The diamond star pattern creates a diamond-shaped pattern using stars.Approach:
Run two nested loops, one for rows and one for spaces and stars.
The outer loop runs for
2 * n - 1
times, wheren
is the number of rows in the diamond.The first inner loop runs for
abs(n - i)
times, printing spaces in each iteration.The second inner loop runs for
2 * min(i, 2 * n - i) - 1
times, printing stars in each iteration.After the inner loops complete, print a newline character to move to the next row.
Code:
Happy Coding!