Find the longest word works in VS Code but not in FCC

Hi guys,

this code works in VS Code but not in FCC and I don’t get why. :stuck_out_tongue:


function findLongestWordLength(str) {
let words = str.split(" ");
let result = "";
for (word of words) {
  if (word.length > result.length) {
    result = word;
  }
}
return result.length;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string

1 Like

Hi,

When using the for ... of loop you have to declare the variable with let, const or var.

Your word variable is not declared the right way.

More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

1 Like

Well…that was a really noob mistake! :open_mouth: thanks a lot!

1 Like