

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to print star pattern in JavaScript in a very simple manner?
Here is a simple star pattern that we are required to print inside the JavaScript console. Note that it has to be printed inside the console and not in the output or HTML window −
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Here’s the code for doing so in JavaScript −
Example
const star = "* "; //where length is no of stars in longest streak const length = 6; for(let i = 1; i <= (length*2)-1; i++){ const k = i <= length ? i : (length*2)-i; console.log(star.repeat(k)); }
Output
The console output will be −
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The String repeat() function is way of telling the compiler to produce a string with n copies of the string it is used in context of, where n is the argument it receives.
The time complexity of this code is O(length^2) and the space complexity is O(1).
- Related Questions & Answers
- Print Pyramid Star Pattern in Java Program
- Java Program to Print Pyramid Star Pattern
- Java Program to Print Diamond Star Pattern
- Java Program to Print Square Star Pattern
- Java Program to Print 8 Star Pattern
- Java Program to Print Inverted Star Pattern
- Java Program to Print X Star Pattern
- Python Program to Print an Inverted Star Pattern
- Golang Program to Print an Inverted Star Pattern
- C program to print hollow rectangle star pattern
- Java Program to Print Left Triangle Star Pattern
- Java Program to Print Upper Star Triangle Pattern
- Java Program to Print Downward Triangle Star Pattern
- Java Program to Print Half Diamond Star Pattern
- Java Program to Print the Left Triangle Star Pattern
Advertisements