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

203
Views
How can I print thunder star pattern in JavaScript?

I have a task to do thunder star pattern loop that look like this:

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

I can only work up to this part:

$n = 5;
for($i = 1; $i <= $n; $i++) {
 for($j = 1; $j <= $n; $j++) {
  if($i == $j) {
    document.write(" * ");
  } 
  else {
    document.write("&nbsp;&nbsp;&nbsp;");
  }
}
  document.write("<br/>");
}

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

0

const diagonalLine = (n) => {
  let line = '';

  for (let i = 0; i < n; i++) {
    for (let j = 0; j < i; j++) {
      line += ' ';
    }
    line += '*\n';
  }
  return line;
};
const line = (n) => {
  let line = '';

  for (let i = 0; i < n; i++) {
    line += '*';
  }
  return line;
};
const printPattern = n => `${diagonalLine(n)}${line(n)}${diagonalLine(n)}`;

console.log(printPattern(5));

about 4 years ago · Juan Pablo Isaza Report

0

can be achieved by adding one way by one more variable for looping of pattern

$n = 5;
$count = 1; // added for looping pattern
for($i = 0; $i <= $n; $i++) {
 for($j = 1; $j <= $i; $j++) {
  if($i == $j&& $i != $n ) {
    document.write(" * ");
  }if($i == $n && $j < $n && $count > 0){
   document.write(" * ");
  }else {
    document.write("&nbsp;&nbsp;&nbsp;");
  }
}
if($i == $n && $j == ($n + 1 ) && $count > 0)
  {
   $j=1;
   $i=0;
   $count--;
  }
  document.write("<br/>");
}

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!