Basic Algorithm Scripting - Find the Longest Word in a String

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

function findLongestWordLength(str) {
  let countArr = [];
  let count = 0;
  let reg =/\s/
  for(let i = 0; i<str.length;i++){
count++;
if(reg.test(str[i])){
  countArr.push(count-1);
  count = 0;

}
  }
  let max = -1;
  for(let i = 0;i<countArr.length;i++){
    if(countArr[i]>max){
      max = countArr[i]
    }
  }
  return max;
}

console.log(findLongestWordLength("What if we try a super-long word such as otorhinolaryngology"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Find the Longest Word in a String

Link to the challenge:

Can you explain what you’re having difficulty with please?
HINT: Try adding a console.log of your countArr after the end of your first for loop… you might be able to see what the issue is.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.