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

208
Vistas
Please is there a way i can print a hollowed rectangle like this one in the picture with javascript

Please I am trying to print out a hollowed rectangle according to what length and width that the user gives to the command but I can't seem to wrap my head around how I can achieve that. I have tried so many iterations of my code below without archiving the required result. Below is the code:

function printRectangle(rows, cols) {
  let i, j;
  for (i = 0; i <= rows; i++) {
    for (j = 0; j <= cols; j++) {
      if (i < 2 || i === rows || j < 2 || j <= cols - 15) {
        process.stdout.write('*');
      } else {
        if (i < 2 || i === rows || j === 2 || j <= cols - 5) {
          process.stdout.write(' ');
        }
        // process.stdout.write(' ');
      }
    }
    process.stdout.write('*****\n');
  }
}

// user imputs
let rows = 10,
  columns = 30;
printRectangle(rows, columns);


I am printing it on the command line using node.js. below is my desired result and what I have been getting: The desired result that I should print out

What I am getting

function printRectangle(rows, cols) {
  let i, j;
  for (i = 0; i <= rows; i++) {
    for (j = 0; j <= cols; j++) {
      if (i < 2 || i === rows || j < 2 || j <= cols - 15) {
        process.stdout.write('*');
      } else {
        if (i < 2 || i === rows || j === 2 || j <= cols - 5) {
          process.stdout.write(' ');
        }
        // process.stdout.write(' ');
      }
    }
    process.stdout.write('*****\n');
  }
}

// user imputs
let rows = 10,
  columns = 30;
printRectangle(rows, columns);

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This would work. I used console.log so that you can view the demo

const spaceWidth = 21;
const spaceHeight = 3;

function printRectangle(rows, cols) {
  let result = '';
  const borderWidth = (cols - spaceWidth) / 2;
  const borderHeight = (rows - spaceHeight) / 2;

  for (let i = 0; i < rows; i++) {
    for (let j = 0; j < cols; j++) {
      const rowIsBorder = i < borderHeight || i >= borderHeight + spaceHeight;
      const colIsBorder = j < borderWidth || j >= borderWidth + spaceWidth;

      result += rowIsBorder || colIsBorder ? '*' : ' ';
    }

    result += '\n';
  }

  console.log(result);
}

printRectangle(9, 31);

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