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

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

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