Find the Longest Word in a String - answer seems correct

Find the Longest Word in a String - answer seems correct
0

Tell us what’s happening:

Hi all,

I tested my solution in Chrome console and it returned the correct result every time. However it is not passing any of the tests on the FreeCodeCamp side. It is returning the following:

assignment to undeclared variable i

Any direction is appreciated. I have tested it with the strings for the tests it must pass and in console it returns the correct answers.

Your code so far


function findLongestWordLength(str) {
  var strSplit = str.split(' ');
  var longestWord = strSplit[0].length;
  for (i = 0; i < strSplit.length; i++) {
    if (longestWord < strSplit[i].length) {
      longestWord = strSplit[i].length;
    } 
  } return longestWord;
}

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

Your browser information:

I am logged into FreeCodeCamp in Firefox but using chrome console to test code.

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0.

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

the error message is correct you never declared i
try this:
for (let i = 0; i < strSplit.length; i++;)

Oops! it’s the darn little things right haha. I haven’t been coding enough lately to remember proper syntax apparently. Chrome was nice enough to overlook that. Thanks for the direction, that worked!

1 Like

hey guys, please can any one guide me on how to do with this stuff, because I’m new here and I don’t know anything yet. thank you

I just started at the beginning and am working my way through the tutorials and challenges. It’s important to take some time and do an internet search when you get stuck, to see if you can figure it out without asking first. It can seem difficult at times but keep at it and you will learn over time.