Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

330
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda