Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
¿Comparando la longitud de las cadenas en una matriz?

Hola, estoy tratando de comparar la longitud de cada palabra en una oración. Convertí esta oración en una matriz, pero luego me perdí en mis códigos. Probé 2 métodos: for loop y reduce(), pero ninguno funcionó. ¿Qué pasó en mi código?

Reduce() => Este me dio indefinido cuando intento ejecutar la función. Deduje que se debe a max.length/word.length, pero ¿cómo puedo convertir mi cadena en longitud usando 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 => Este me dio la longitud de la primera palabra en lugar de maxLength. Supongo que probablemente porque mi var strLength no es una matriz, pero ¿cómo convertirla en una matriz que contenga la longitud de cada índice?

 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");

Sé que esto debería ser muy básico y simple y no puedo entender qué salió mal aquí.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

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 Report

0

En el segundo enfoque que publicó, no está configurando el máximo correctamente. Está configurando una variable llamada strLength , pero su Math.max está fuera del ciclo. Por lo tanto, nunca obtendrá el valor máximo a menos que sea la última palabra de la matriz.

 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 Report

0

Si desea utilizar el enfoque de Math.max , primero extraiga las longitudes de cada palabra y luego páselas a 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"));


Si desea utilizar el enfoque for , tendrá que mantener una longitud máxima durante el bucle y actualizarla a medida que el bucle encuentre palabras más largas.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!