Why my code Basic Algorithm Scripting: Find the Longest Word in a String not work

Why this isn’t correct!?

function findLongestWordLength(str) {

let x = str.split(’ ');let backword=0;

x.filter((word1)=>{

for(let i = 0;i<x.length;i++)

{

if(word1.length <= x[i].length)

   {

     backword=x[i].length;

   }

}

}

)

}

Your code so far


function findLongestWordLength(str) {
let x = str.split(' ');let backword=0;
x.filter((word1)=>{
for(let i = 0;i<x.length;i++)
{
  if(word1.length <= x[i].length)
     {
       backword=x[i].length;
     }
}
}
)
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:

Hi,

Your code involves way too many complicated tools than what is necessary.
I wrote my code for this question and solved it, take a look.

I took a look at your code for about 5 seconds and gave up in trying to understand it, because I think it is way too complicated than what’s necessary, and since time is money, writing easy, short, straight forward code is a must.

Let me be clear that, I am not criticizing your coding ability, you have demonstrated your knowledge of knowing some high level function like “filter”, which is good, but sort of an overkill for this level of question.

As you get more experienced, you’ll develop a way of understanding how to approach questions like this the easiest possible way, with your knowledge.

function findLongestWordLength(str) {
  var arr = str.split(' ')
  var word = arr[0]

  for(let i = 0; i<arr.length; i++){
    if(arr[i].length > word.length){
      word = arr[i]
    }
  }

  return word.length
}

This question tests your ability to break down a string of words into an array, then comparing each array item by their lengths. That’s all there is, nothing more.

Remember, the easier your solution, the shorter your code, the better.

Cheers,

1 Like

I have rewritten your code to make it simpler

function findLongestWordLength(str) {
  var arr = str.split(' '),ret=0;
  for(let i = 0; i<arr.length; i++) if(arr[i].length>ret)ret=arr[i].length;
   return ret;
}
1 Like

I think that, your code and mine have the same number of lines if they are structured the same way? Also you don’t use curly braces, which I’ve seen before but never understood why people would lose the curly braces.

Anyhow, looks good to me, in the end if it works and it’s easy to understand, it’s good.

Solution: (This question tests your ability to 1)break down a string of words into an array, 2) comparing each array item by their lengths. )
honest! … beautiful thinking to simplify the question.
This is my fifth day of JavaScript programming …Is my level good? Or should I change the way I think about the problem?

I suggest you come back to this post, 50 days later, and answer your question yourself. No one can tell you if you are good or bad, or what the correct way to think is, except yourself.

1 Like

The purpose of freeCodeCamp is to learn how to solve problems with code, and the purpose of the freeCodeCamp forums is to help you develop your own solutions.

In this case, I think that you had the right ideas in your first post but weren’t quite putting the pieces together correctly. Someone should have been able to guide you to fix up that code.

Ultimately, it’s OK if your code isn’t the most elegant code ever written because you will learn and improve if you keep at this. Ask questions, have people give you hints, keep working and your code will improve. We’re all here to learn!

1 Like