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

321
Views
function that takes a list of strings and prints them, one per line, in a rectangular frame
  1. Write a function that takes a list of strings and prints them, one per line, in a rectangular frame. For example the list ["Hello", "World", "in", "a", "frame"] gets printed as:
    *********
    * Hello *
    * World *
    * in    *
    * a     *
    * frame *
    *********

my code

    var x = "hello\nworld\nin\na\nframe";

function star(str) {
  let arr = [];
  arr = str.split("\n");
  for (let index = 0; index < 1; index++) {
    console.log("*******");
    for (let j = 0; j <= arr.length; j++) {
     arr == arr[j].split(",");
      console.log("*" + arr[j] + "*" );
    }
  }
  console.log("******");
  return arr;
}
console.log(star(x));
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

This is how I would do it, but I'm sure there are better ways.

function makeStarBox(arr){
  const longest = arr.reduce((a, b) => a.length <= b.length ? b : a);
  const box_width = longest.length + 2;
  
  console.log("*".repeat(box_width));

  arr.map(str => console.log("*" + str + " ".repeat(box_width - (str.length + 2)) + "*"));

  console.log("*".repeat(box_width));
}

It does seem like you are posting an actual homework/exam question though, so make sure to study the code and learn for yourself what it actually does. If you don't know what map, reduce or repeat does, check out the documentation and learn:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat

about 4 years ago · Santiago Gelvez 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!