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

155
Visualizações
How to return an array of numbers that represent lengths of elements string?

If an array was: ['hey', 'you', 'muddy'] The expected output should be: [3, 3, 5]

This is what I have so far:

function lengths(arr) {
  numbersArray = [];
  for (var i = 0; i < arr.length; i++) {
    numbersArray = arr[i].length;
  }
}

Any help would be much appreciated.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need to push the length of every item (using Array#push) and return the array in the end:

function lengths(arr) {
  const numbersArray = [];
  for (let i = 0; i < arr.length; i++) {
    numbersArray.push(arr[i].length);
  }
  return numbersArray;
}

console.log( lengths(['hey', 'you', 'muddy']) );

Another solution using Array#map:

function lengths(arr) {
 return arr.map(str => str.length);
}

console.log( lengths(['hey', 'you', 'muddy']) );

about 4 years ago · Juan Pablo Isaza Relatório

0

const getStringLength = (arr) => {
  return arr.map((item) => item.length);
}
console.log(getStringLength(['hello', 'world']));

about 4 years ago · Juan Pablo Isaza Relatório

0

function lengths(arr) {
  return arr.map(el => el.length)
}

Array.map is commonly used in such things. Docs

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