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

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!