Help with "Find the longest word"

I’ve almost finished doing Longest word but I’ve got stuck with the loop. It should count length of EACH word, not just the one listed in for parenthesis. Could you pls help me out here? My code is below:

function findLongestWord(str) {
str = str.split (’ '); //array of separate words
for (var x=0; x<str.length; x++) {
return str[x].length; //for some reason it doesn’t go through each item in array, like it should 'coz of x++

findLongestWord(“The quick brown fox jumped over the lazy dog”);

it dose what its coded to do it returns the length of first word and finishes … if you want to see all lengths you should console.log(str[x].length)
so you should get rid of the return … and instead compare the length of str[x] to max … then if its larger than max set str[x] to max … if its smaller than max do nothing then when for loop is over return max outside of the for loop
as for max … create a variable before the for loop eg max = 0;
here is a visual of your code as is … you can step through it and see what is going on
each step look to the right side of page to see the changes …

1 Like

Huge thanks! Finally got it. Code visualizer seems like handy thing as well.