Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
How to print '*' pattern printing using JavaScript loop?

I am learning loop in Javascript.

I need output like this:

*
**
***
****
*****

But, When I use this code :

for (i = 1; i <= 5; i = i + 1) {
  for (j = 0; j < i; j = j + 1) {
    console.log('*');
  }
  console.log("");
}

I got an output like this :

*

*
*

*
*
*

*
*
*
*

*
*
*
*
*

How do I solve this?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

In JS, every console.log() is printed on a new line. So you will have to keep a string which you keep adding to. And then print it:

for (i = 1; i <= 5; i = i + 1) {
  let str = "";
  for (j = 0; j < i; j = j + 1) {
    str += "*";
  }
  console.log(str);
}

about 4 years ago · Juan Pablo Isaza Report

0

You don't need a second loop. You can achieve the result by using 1 loop and the repeat method.

for(i=1;i<=5;i=i+1){
    console.log('*'.repeat(i));
}

Edited according to @trincot. Thanks.

about 4 years ago · Juan Pablo Isaza Report

0

Single Loop Solution with String.prototype.repeat()

In JavaScript, console.log() prints output on a new line, so you need to concatenate all the stars in a string and print it.

const printPattern = (count, symbol) => {
  for (let i = 1; i <= count; i++) {
    console.log(symbol.repeat(i));
  }
};

printPattern(5, '*');

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!