Find the Longest Word in a String help me

I’m stuck not sure how to finish my code i want it count the arr and then change longest word and stop at the biggest one then return the number but i’m blank.

Your code so far


function findLongestWordLength(str) {
 var count =0;
 var longestWord =0;
 arr= str.split(" ")
 }
 for (var i=0; i<= arr.length;++i);{
   longestWord = (arr.length)
 }

  

  return str.length;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0.

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

Think, you are checking the words in the string one by one, when you need to change longestWord?

Update:

 var count= str.split(" ");
 var longestWord =0;
 
 }
 for (var i=0; i<= arr.length;++i);{
   longestWord = arr.length
 }

  

  return str.length;
}

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

update 2:

  var longestWord = 0;

}
for (var i = 0; i <= count.length; ++i); {
  longestWord = count.length
}




return str.length;
}

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

i’m really lost didn’t want to look at the hint

it equaling to my count

You are just setting it to the same thing at each iteration of the loop.

Forget code ab try to answer my previous question

i give up I reset the code try again another day :woozy_face:

@royalgreen50 Before writing any more code, I suggest you try to write out an algorithm first which will be all the steps needed to solve the challenge. You can validate your algorithm works by manually walking through the steps for each test case. Once you are sure it works, then attempt to write the necessary JavaScript code.

I’m going to walk through the code you made line by line:

function findLongestWordLength(str) {
}

declares a function in global scope.

var count =0;
// never use var,. only use const or let. const can’t change its value, let can.

since you plan to change its value,. use let.

var longestWord =0;
same issue as before.

so before I continue,. we have two variables:
count & longestWord. Both are 0. Remember that.

arr= str.split(" ")
This has a small issue. You’re making arr globally, because you dont have let or const in front of it. Avoid globals at all costs! Whenever you’re assigning a variable use const or let. The only time you wouldn’t is if it’s already declared.

Example:

let arr;  <-- this is how you declare a variable
arr = str.split(' ');

let arr = str.split(' ');
this declares arr and assigns a value to it. It basically does both lines above into one.

This example would be ok,. because arr was declared beforehand.

It seems you know what split does, but let’s type it out what you just gave arr.

arr now equals ['The', 'quick', 'brown', 'fox', ....];
What would that length be? Well count each word. You should count 9. Remember 9.
}
Is this a typo?? By adding this,. you effectively close this function. Why? Well look for the opening bracket… You wont find one until you get to findLongestWordLength(str){…

for (var i=0; i<= arr.length;++i);{
   longestWord = (arr.length)
 }

Now here’s a loop. You’re going from 0 to arr.length (including its length). Now,. in just about every situation,. you don’t want to include it’s length too. Reasoning is because you started at 0 and not 1.

Example:
[4, 5, 6, 7].
The length here is 4.

If you did a loop from 0 to 4 (because you did <= arr.length)
That would be 5 times: 0, 1, 2, 3, 4

Next, look at what you’re doing in your loop.
You’re assigning longestWord 5 times to the value of 9. Why 9? Remember the length of arr was 9.

Finally., you’re returning the str.length. So all that work your function is doing,. means almost nothing, because at the end you’re always returning the length of the string passed into the function.

@kerafyrm02 I’m not getting JavaScript at all i don’t understand it or why I even need it its all math function I slept in math class in high school. Idk what too do everything comes out wrong.

I’ m going to star over this morning I’m trying not to just copy the hint.

If you’re weak at something, do something about it.

If math is your weakness,. then learn math online. Yes,. even if that means starting with high school math. No reason to rush it. There’s no test at the end of the week. Read it multiple times if you have to until you get it. Some people feel like after reading something 4-5x to simply skip it if they don’t get it. That’s exactly what you don’t do. You should stay right at that same spot until you get it. Why? Because that concept will likely have something else build upon it.

The most important part of it all., is don’t quit.

Need some inspiration? https://www.youtube.com/watch?v=tD-VuwulVgI

1 Like

@kerafyrm02 i just copied the hint it has var all over it so i was doling it right you have confuse me for really.

function findLongestWordLength(str) { 
   var words = str.split(' ');
  var maxLength = 0;

  for (var i = 0; i < words.length; i++) {
    if (words[i].length > maxLength) {
      maxLength = words[i].length;
    }
  }

  return maxLength;
}
  


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

I may just quite all of this, basic was was so much easier why can’t we just use that.

so this is my answer i wish i could understand what its doing.

function findLongestWordLength(str) { 
   var count = str.split(' ');
  var longestWord = 0;

  for (var i = 0; i < count.length; i++) {
    if (count[i].length > longestWord) {
      longestWord = count[i].length;
    }
  }

  return longestWord;
}
  


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

I am not sure how you create a solution but not understand how it works. Why don’t you try to explain to us what you think it is doing. We can correct you if you are wrong.

Also, I have blurred out your solution to prevent spoilers for others.

Unfortunately FCC has not updated its syntax to be in line with the current best practices. Using var allows you to write buggy code that using const and let will prevent.

I would but I cant see it not sure why you did that

Blurred text can still be showed if you click or tap on it

The blurring could be to avoid spoilers to other readers

I don’t feel my needs spoiler tags around it, as it contains lots of breaks with text that would require someone read it in order to spoil it for them. This post is designed to be helpful, and not to just give the answer with no idea what is happening.


Here is line by line what your code is doing. I had to retype it all since I can’t copy from spoiler tags, so there might be a couple of typos. This is a challenge people have a hard time with, as it used newer concepts like embedded loops.

The code used is from comment #15

function findLongestWordLength(str) {

Basic function set up w/ a parameter. The desired input for str is a senetnce and each word is seprated by a space. Lets say str is "hello my name is"

var count = str.split(' ')
var longestWod = 0

count is now a array and each item (element) in the array is a word.

["hello", "my", "name", "is"]

If you did .split('') which has no space, then it would create an array where each element is a character. We don’t want this.

["h","e","l","l","o", ""] (etc)

longestWord is just a variable that will be used later. It needs to be declared outside the loop so you can reference it inside of the loop without resetting its value. For instance:

for (var i = 0; i < 5; i++) {
  longestWord = "hello"
  var longestWord = 0;
}

You can see that it would never hold the value "hello" because it would be reset at the end of each loop.

for (var i = 0; i < count.length; i++) {

I want this loop to run as many times as count is long. I start at 0 on the first loop because the index of an array starts at 0 for the first element. i is the counter variable (CV) which is what I will be calling it from now on.

if (count[i].length > longestWord) {

count[i] is how accessing my array with all my words and goes to the word that corresponds with my CV. When the CV is 0, then it will be count[0], which goes to the first word which is "hello".

So .length will then get the length of that word/element which is 5. If my CV was 1, it would go to my second element of count and get a length of 2 for “my”.

I then compare it to longestWord which is a number and ask if it is greater then that. The default value is 0, so the first word/ element will always be bigger.

longestWord = count[i].length

Now if I compare the two and it is true, then I assign that length, 5 in the case of “hello” to longestWord.

This then continues on for every word in the array until my CV is 5, and the loop ends and I return longestWord, which is a number.


Have any questions? Please ask!