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

109
Visualizações
Comparing length of strings in an array?

Hi I'm trying to compare the length of each word in a sentence. I converted this sentence into an array but then I'm lost in my codes. I tried 2 methods - for loop and reduce(), but neither worked. What happened in my code?

Reduce() => This one gave me undefined when I try to run the function. I gathered it's because of the max.length/word.length, but how can I turn my string into length using reduce()?

function findLongestWordLength(str) {
  let brokenDown = str.split(' ')
  
  brokenDown.reduce((max, word) => {
    return Math.max(max.length, word.length)
  })
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");

For loop => This one gave me the length of the first word rather than the maxLength. I'm guessing probably because my var strLength is not an array, but how to make it into an array containing the length of each index?

function findLongestWordLength(str) {
  let brokenDown = str.split(' ')
  for(let i = 0; i < brokenDown.length; i++) {
    var strLength = brokenDown[i].length
  }
  return Math.max(strLength)
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");

I know this should be very basic and simple and i can't wrap my head around what went wrong here.

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

0

const maxLength = (str) => {
  const words = str.split(' ');
  const lengths = words.map((word) => word.length);
  return Math.max(...lengths);
}
console.log(maxLength('The quick brown fox jumped over the lazy dog'));

about 4 years ago · Juan Pablo Isaza Relatório

0

In the second approach you posted, you aren't setting the max properly. You are setting a variable called strLength, but your Math.max is outside the loop. So you will never get the maximum value unless it happens to be the last word in the array.

function findLongestWordLength(str) {
  let brokenDown = str.split(' ')
  let mxLen = 0
  for(let i = 0; i < brokenDown.length; i++) {
    mxLen = Math.max(mxLen, brokenDown[i].length);
  }
  return mxLen
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If you want to use the Math.max approach, first extract he lengths of each word and then pass those to the Math.max.

function findLongestWordLength(str) {
  let brokenDown = str.split(' ')
  let lengths = brokenDown.map(word => word.length);

  return Math.max(...lengths);
}

console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"));


If you want to go the for loop approach, you will have to keep a maxLength while looping, and update it as the loop encounters longer words.

function findLongestWordLength(str) {
  let brokenDown = str.split(' ')
  let maxLength = 0;
  for (let i = 0; i < brokenDown.length; i++) {
    const wordLength = brokenDown[i].length;
    if (wordLength > maxLength) {
      maxLength = wordLength;
    }
  }
  return maxLength;
}

console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"));

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