Find the Longest Word in a String: How do i make this work

function findLongestWordLength(str) {
  let temp = 0;
  let count = 0;
  for(let i = 0; i <= str.length; i++){
    if(str[i] !== " "){
      count++;
    } else if(count >= temp){
      temp = count;
      count = 0;
    } else if(count < temp)
      count = 0;
  console.log("temp is " + temp + " for String at " + i + " and count is " + count);
  }
  return temp;
}
findLongestWordLength("What if we try a super-long word such as otorhinolaryngology");

Look at the test condition in your for loop. Is that where you want to stop?

Also, temp only gets set to count if the current character is a space. What about the end of a string? Does that really need to be an if else statement or should you check every iteration?

Lets take a look at this another way:

A string can be split at various points. When we split a string, we end up with an array of substrings. We decide where we split a string.


function splitAString(string) {
   // Here, we choose to split the string at the "-" character
   arrayOfSubStrings = string.split('-'); 
   console.log(arrayOfSubStrings);
}

splitAString("here-is-an-example"); // ["here", "is", "an", "example"]

Hint: So now we have an array of strings. What other property does a string have that would be useful once we split the string?

the length of a string ? thanks

I saw a solution to my problem using reduce. But i want to make this work. count at the end of the string it my problem

Hi,

Reduce is a good way to solve this challenge but it is good that you curious enough to want to make this work too.

<= str.length won’t work because length is always one more that the largest index. So if i is equal to str.length you are one past last index and count will be one too many.

Your code fails when the longest word is the last word in the string.
If you moved the update to temp if(count >= temp) separate from the if else it would be evaluated every iteration so you would always end with an accurate value for temp even if the longest word falls at the end of str

Here is your code with some notes

function findLongestWordLength(str) {
  let temp = 0;
  let count = 0;
  for(let i = 0; i <= str.length; i++){  //if i equals str.length you are past the end of str
    if(str[i] !== " "){
      count++;
    } else if(count >= temp){  //anything past here won't happen at the end of str
      temp = count;            // so temp will not be set equal to count
      count = 0;
    } else if(count < temp)
      count = 0;
  console.log("temp is " + temp + " for String at " + i + " and count is " + count);
  }
  return temp;
}

THANKS.

this really helped.

function findLongestWordLength(str) {
  let temp = 0;
  let count = 0;
  for(let i = 0; i <= str.length; i++){  //if i equals str.length you are past the end of str
    if(str[i] !== " " && i != str.length){
      count++;
    } else if(count >= temp){  //anything past here won't happen at the end of str
      temp = count;            // so temp will not be set equal to count
      count = 0;
    } else if(count < temp)
      count = 0;
  console.log("temp is " + temp + " for String at " + i + " and count is " + count);
  }
  return temp;
}