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

207
Views
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 answers
Answer question

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 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!