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

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

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 Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

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