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

117
Visualizações
For Loop within a function to add characters to a string doesn't return the characters added within the loop

I am trying to create an ascii type table to output to the console. One of the columns will have an input column header, a persons name. Since name length will vary, I wanted to create a dynamic way to build the table etc.

Here is a hypothetical goal:

+---------+---------+------------+
|  Round  |  Bruce  |  Computer  |
+---------+---------+------------+
|    1    | X       |            |
|    2    |         | X          |
|    3    | X       |            |
|    4    | X       |            |
|    5    |         | X          |
+---------+---------+------------+

In order to get the columns to line up etc, they'll need to be able to vary. Just starting, I wanted to create the horizontal 'border' and I tried the following:

let dasher = (nameLength) => {
    let temp = '----';
    for (let i = 0; i < nameLength; i++) {
        temp + '-';
    }
    return temp;
}

let hr = `+---------+` + dasher(playerName.length) + `+------------------+`;

However, I only get the 4 initial '-', but no additional '-'. The output is:

+---------+----+------------------+

But the desired output for the name Bruce, should be:

+---------+---------+------------------+

I am certain it has something to do with JS's asynchronous behaviours, just don't know how or how else to get what I am looking for

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