Modify Problem Explanation: Find the Longest Word in String

Hello, I’d like to purpose someone modify the problem explanation text on this page: freeCodeCamp Challenge Guide: Find the Longest Word in a String

New learners will not be able to solve this problem without split method. However, split method comes after this challenge. Please alert learners to use a code concept they have not encountered yet.

Under relevant links, please add split method: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/split-a-string-into-an-array-using-the-split-method

3 Likes

Thank you, for pointing this out.

I suggest, in this case: We change the hint to revolve around a solution which does not use the split method:

Solution
function findLongestWordLength(str) {
  let record = 0;
  let i = 0;
  let wordLength = 0;
  while(true) {
    wordLength = str.indexOf(' ', i) - (i);
    if (str.indexOf(' ', i) === -1) {
      wordLength = str.length-i;
    }
    i = i + (wordLength > 0 ? wordLength : wordLength*(-1)) + 1;
    // OR...
    // i += Math.abs(wordLength)+1;
    if (wordLength > record) {
      record = wordLength;
    }
    if (i > str.length) {
      break;
    }
  }
  return record;
}

It is not pretty, but I am sure with a bit more thinking someone can make a simpler version using a for...of loop or something.

Thanks @Sky020 – Is “Math.abs” a special JS thing? If so, we haven’t learned that either up until this point in the course.

Ah…well no. It Math is a core module from the JS library. I am not sure if it is taught in the fCC curriculum, but it is definitely mentioned somewhere.

At the end of the day, some of the challenges do require a small amount of external research from campers. But, this is why we have the guide posts.

So, for now, I will add the above solution (i changed it to exclude Math.abs) as the first solution, and leave the other solutions, as they are still valid.

Also, the simplest solution would probably involve regex:

// Loop through each match
function findLongestWordLength(str) {
  const arrayOfStr = str.match(/(?<=\s*)\w+/gi);
  // Loop through
}

Thanks so much, Sky!

Hi @Sky020, Could you give us a hint of what we can do inside of the for...of loop?

have you checked how a for of loop works?

I have not learned for...of loop in the freecCodeCamp Curriculum JavaScript yet.
I searched about that but I didn’t understand the for...of loop

I was able to complete the challenge without using split.

function findLongestWordLength(str) {
  let counter = 0;
  let maxLenght = 0;
  str = str + " ";
  
    for(let word in str) {
      if(str[word] != " ") {
        counter++;
    } else {
      if(counter > maxLenght) {
          maxLenght = counter;
      } 
      counter = 0;
    } 
  }

  return maxLenght;
}

I did not know about split method.

1 Like

Hi @andrucasa1 !

Welcome to the forum!

I have added spoiler tags to your code for those who have not worked on the problem yet.

1 Like

I completed the challenge using for loop and if else statements. I didn’t know of split() method during the challenge.

solution
function findLongestWordLength(str) {
    let count=0;
    let max=0;
    for(let i=0;i<str.length;i++)
    {
        if(str[i]!==' ')
        {
            count++;
        }
        else
        {
            if(count>=max)
            {
                max=count;
                count=0;
            }
            else
            {
                count=0;
            }
        }
    }
    if(count>=max)
    {
        max=count;
    }
    return max;
}

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

console.log(x);
1 Like