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

155
Vistas
How to find longer Strings from an Array?

I want to find Longer Strings from this array:

const myArr = ["first", "second", "third", "fourth", "fifth", "sixth", "seven", "eighth"]

in this array "second","fourth","eighth" has length of 6. I want to return these in an array.

const myArr = ["first", "second", "third", "fourth", "fifth", "sixth", "seven", "eighth"]

function longerStrings(arr) {
    let largeStrings = []
    let longerWord = ''
    for (let name of arr) {
        if (name.length > longerWord.length) {
            longerWord = name
            largeStrings.push(longerWord)
        }
    }
    return largeStrings
}
longerStrings(myArr)

expected output is: ["second","fourth","eighth"]

but returns =["first","second"]

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

When a word longer than any found so far is identified, you need to create an entirely new array (discarding any elements it had before in it), and put the new record-setting word in it. You also need to push a name to the array if the name being iterated over isn't record-setting, but is equal to the current record-setter.

const myArr = ["first", "second", "third", "fourth", "fifth", "sixth", "seven", "eighth"]

function longerStrings(arr) {
  let largeStrings = []
  let longerWord = ''
  for (let name of arr) {
    if (name.length > longerWord.length) {
      longerWord = name
      largeStrings = [name];
    } else if (name.length === longerWord.length) {
      largeStrings.push(name)
    }
  }
  return largeStrings
}
console.log(longerStrings(myArr));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Easier if you use filter instead:

const myArr = ["first", "second", "third", "fourth", "fifth", "sixth", "seven", "eighth"]
const longest = myArr.reduce((a, b) =>  a.length > b.length ? a : b);
const res = myArr.filter(el => el.length >= longest.length)
console.log(res)

That returns:

["second","fourth","eighth"]

Or, as Sash mentions below, it can be done in a single pass, although the solution is not as readable:

let max = 0;
const reducer = myArray.reduce((previous, current) => {
  if (current.length > max) {
    previous = [];
  }
  if (current.length >= max) {
    previous.push(current);
    max = current.length;
  }
  return previous;
}, []);

console.log(reducer);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could just sort the array by length descending and then select only values which have the same length as the first element:

const myArr = ["first", "second", "third", "fourth", "fifth", "sixth", "seven", "eighth"]

const result = myArr
  .sort((a, b) => b.length - a.length)
  .filter((v, _, arr) => v.length == arr[0].length)
  
console.log(result)

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