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

122
Visualizações
Repeat character depending on position in array + 1

I'm trying to get an output like this:

['h', 'ee', 'lll', 'llll', 'ooooo']

currently my out put is:

[ 'h', 'ee', 'lll', 'lll', 'ooooo' ]

The issue is the second occurrence of the "l" isn't being repeated one more time because I'm counting the index of the letter then adding 1 and it is only counting the first occurrence of the "l". This is the code I have so far, any help would be great.

function mumble(string) {
  string.toLowerCase();
  let arrayPush = [];
  let array = string.split("");
  let count = 0;
  let char = [];
  array.map((letter) => {
    count = array.indexOf(letter);

    arrayPush.push(letter.repeat(count + 1));
  });

  return arrayPush;
}

console.log(mumble("hello")); 
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Don't use indexOf, use the second parameter in the .map callback to determine the index (and from that, the number of times to repeat the string).

function mumble(string) {
  string.toLowerCase();
  let arrayPush = [];
  let array = string.split("");
  let count = 0;
  let char = [];
  array.map((letter, i) => {
    arrayPush.push(letter.repeat(i + 1));
  });

  return arrayPush;
}

console.log(mumble("hello")); 

about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of applying .map to an array split from the string, you can simply loop through the string, accessing each character using the .charAt() string method, and pushing it's .repeat() product to the result array.

Working snippet:

console.log(mumble('hELlo'));

function mumble(string) {
const result = [];

for(let i=0; i<string.length; i++) {
    result.push(string.toLowerCase().charAt(i).repeat(i+1));
}

return result;
} // end function mumble;

about 4 years ago · Juan Pablo Isaza 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