Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

297
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda