Find longest word in a string - problem with my code

Could someone tell me whats wrong with this?

function findLongestWordLength(str) {
  var arr = str.split(" ")
  var max=arr[0].length;
  for (i=1; i<arr.length;i++){
  if (arr[i].length>max){
    max = arr[i].length;
  return str.length=max;

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

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


In the future, please create a new topic if you are having trouble with a challenge and it is best to just use the Ask for Help button located on the challenge, because it will include your code and a link to the challenge url.

Your solution has a few problems. First, you should declare the variable i in your for loop. Second, you can not assign a new value to a read-only property of an object as you are trying to do below.

return str.length=max;

If max contains the largest word length, then just return max.

1 Like

Thanks a lot. I will remember these in the future. :grinning::grinning: