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

294
Views
program for printing 'Square' pattern in javascript

I have tried printing 3*5 box * (star) pattern program, but not able to get it to format properly. Can any one guide me mistakes if and help to rectify?

Below is the javascript code:

// Getting input via STDIN
const readline = require("readline");

const inp = readline.createInterface({
  input: process.stdin
});

const userInput = [];

inp.on("line", (data) => {
  userInput.push(data);
});

inp.on("close", () => {
  //start-here
var i,j,a=3,b=5;
for(i=1;i<=a;i++)
{
    for(j=1;j<=b;j++)
    {
        console.log("*");
    }

    console.log("\n");

}

  //end-here
});

output:

*
*
*
*
*

*
*
*
*
*

*
*
*
*
*

need to get like this output:

*****
*****
*****
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

console.log automatically logs to a new line. You should instead append to a variable and log it outside of the loop:

var i, j, a = 3,
  b = 5;
for (i = 1; i <= a; i++) {
  var str = "";
  for (j = 1; j <= b; j++) {
    str += "*";
  }
  console.log(str + "\n");
}

Or even better, don't use a loop at all and use String.repeat:

var i, j, a = 3,
  b = 5;
for (i = 1; i <= a; i++) {
  console.log("*".repeat(b) + "\n");
}

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!