Find the Longest Word in a String-need review and help

Tell us what’s happening:
hello, to get to accomplish this task, i have decided to execute it through 3 step:
first i split the argument of the function and put it in a var;
second i create a new empty variable that takes the indexes of these var;
third i return the max() method on those function so i can see what is the number of the greatest word;

please can you review the code and let me know if my code logic is following what i intend to do and where i have mistkan

I will keep on checking it

Your code so far

function findLongestWord(str) {
  var str_split =str.split(" ");
  var v_list ;
  for (v = 0; v<str_split.length; v ++){
  return v_list.max();

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

Your browser information:

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

Link to the challenge:

  • Your str_split is already an array, so the only thing that your for loop is doing is copying one array to another array.
  • You are not using max() correctly.

i have changed this code by doing :

and still the code is not working properly, may i know what do you think is wrong …i want to take the length of the current item of the array and copy it into an array and finnaly apply the Max() to it

You’re still just copying from one to the other, but this time you are creating 2 arrays every time your for loop executes.

1 Like

Try printing each variable so that you know what you’re actually doing.
What is str_split?
It is an array where each element is a word in your sentence.
What do you want to find?
The longest word.
What do you need?
The length of each word.
That is you need length of each element of str_split.
Store the length of each word in v_list
Find the index of the largest number in v_list.
This index is i, say
Now return from str_split the word at index i

PS: We can give you the code. But it’s better if you do it yourself.


can you explain why i am copying each time in a thorough way to let me see
Please do an explanation as you are giving to a kid of 5
thank you for your help

i will do thank you for your involvement and i appreciate and like your PS

function findLongestWord(str) {
  var str_split = str.split(' '); ===> output = ["The", "quick", "brown", "fox", "jumped", "over","the"," lazy"," dog"]
  var v_list = []; ===> output = nothing at this stage, its a blank list.
  for (var i = 0; i < str_split.length; i++) { ===> its loop through the  length of str_length and will keep doing as long as i is not equals to the length of str_length in this case 9.
    v_list.push(str_split[i].length); ===> the length of the current index is update on the v_list by using the push method.
  return Math.max(v_list);  ===> it should return the max of the list "v_list"
findLongestWord("The quick brown fox jumped over the lazy dog");

after doing everything as asked, the final result equals to “null”

Who can tell me the bug or mistake i did on at this stage.

thank you in advance

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


The only line which is a problem at this point is your return statement (seen below).

return Math.max(v_list);

As @ArielLeslie mentioned in her original reply to your topic, you are not correctly using Math.max. You can not directly use Math.max on an array, because it expects arguments to be supplied to it (not an array). You have a couple of options on how to pass the array elements as arguments to the Math.max function. Take a look at the Math.max documentation for examples of how to do this.