Want to find Longest Word in a String

Please check my code below. Its not running.

function findLongestWordLength(str){
  let newArray = str.split(" ");
  let maxLength = 0;
  for (let i=0 ; i<newArray; i++){
    if (newArray[i] > maxLength)
    maxLength = newArray[i].length;
   return maxLength;

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

Calm down and check two below lines one more time, you will know the reason. In my opinion, the best way to debug code is console.log everywhere I am confused.

for (let i=0 ; i<newArray; i++) {
if (newArray[i] > maxLength)

@6in Thank you, I had figure out the mistake. .length word was missing in my both lines.

I’ve spent almost 2 hour in a single challenge. is it ok?

Spending that much time on a problem is normal. When I first started tackling JavaScript algorithms, it sometimes took days to figure something out, it’s all part of the learning experience.

1 Like

Meow, sure, it’s not ok but very ok :smile: because I can say that from now you will always remember about length of array and string, mistakes make you stronger.