Longest word in a String Missing semicolon

Tell us what’s happening:
My plan is to:

  1. Convert the string to an array
  2. Count the characters in each word
  3. store the values to compare
  4. compare the word lengths
  5. return the longest word in the string
    I realize this may not be the most logical or quickest way, but this is the process that came to mind.

I am really having a hard time understanding for and for/of loops. I feel like I need to do that in some way shape form and fashion here. And I cannot for the life of me figure out where my missing semicolon is…

  **Your code so far**

function findLongestWordLength(str) {
let words = str.split(' ');
String longestWord = str[0];
for(int i = 1; i < str.length; i++) {
  String word = str[i];
  if(word.length() > longestWord.length())
      longestWord = word;
}
return word.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_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:

from here probably

in JavaScript you declare a variable with var (or let or const)

String is an object

it can’t parse what you have written so the editor best guess is that you didn’t divide two statements

Thank you for that! But also, I keep going back and forth through the code and cannot seem to find the missing or extra comma.

Have you tried replacing String with let?

I did. here is my code now. It keeps returning that i is not defined

function findLongestWordLength(str) {
let words = str.split(' ');
let longestWord = str[0];
for(i = 1; i < str.length; i++) {
    let word = str[i];
    if(word.length() > longestWord.length()){
        longestWord = word;
    }
}
  return word.length;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");

That’s because i is not defined :slight_smile:
You need to define every single variable. With a for loop, that’s often done as

for (let i = 0; cond; increment)

I corrected that now it says _word.length is not a function.
im comparing it to one of the solutions, but i still don’t get where I am screwing this up.

function findLongestWordLength(str) {
let words = str.split(' ');
let longestWord = str[0];
for(let i = 1; i < str.length; i++) {
    let word = str[i];
    if(word.length() > longestWord.length()){
        longestWord = word;
    }
}
  return word.length;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");

Vs

These syntaxes should match


I really recommend against comparing to solutions until you have completed the challenge on your own.

When I change str.length() to str.length, it says longestWord.length() is not a function. When i remove the parenthesis, it returns an error "unexpected token, expected 6:40.

You should not have the (). What is your code without the ()?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length

Also, words contains your string split into words, not str.

I realize i removed the parenthesis from around the If statement on line 6. Now it says word is not defined.

function findLongestWordLength(str) {
let words = str.split(' ');
let longestWord = str[0];
for(let i = 1; i < str.length; i++) {
    let word = str[i];
    if(str.length > longestWord.length){
        longestWord = word;
    }
}
  return word.length;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");

str[0] is the first character of str. You want to use words.

The variable word is not in scope here, and it’s probably not the variable you meant to reference.

I thought I defined it in line 5. So that the next word in the array, If it is longer than the previous, is retained and the other is “forgotten”.

word is defined inside of the loop. Once the loop ends, the variables defined inside of the loop (in the loop body or loop head) go out of scope and are no longer defined.

And, the last value word held was the last character in str, so it does not contain the last word in the string or the longest word in the string.

aww crap… So how do I hold the whole word?

This is what your code currently does:

function findLongestWordLength(str) {
let words = str.split(' '); // split the string on spaces
let longestWord = str[0]; // set the longestWord to the first character of str
for(let i = 1; i < str.length; i++) { // loop from 1 to the end of str
    let word = str[i]; // define word as the i'th character in str
    if(str.length > longestWord.length){ // compare the length of str and longestWord
        longestWord = word; // set longestWord to word (which is the i'th character of str)
    }
}
  return word.length; // return the length of the last character in str, if this variable was in scope
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
1 Like

So str needs to be changed to words and define word outside the loop?

Why you you want to define word outside of the loop? Which word do you want to return the length of?

I think it might be worth setting aside the code for a moment and thinking about how you would do this by hand. What would you loop over (look at one by one), what would you be counting, and what would you keep track of?