When I was looking for the longest word in the string and replying to a number in the JS basic algorithm script, I encountered an error, please help, thank you!

Tell us what’s happening:
Describe your issue in detail here.

When I was doing the task of finding the longest word in the string in the JS basic algorithm script and replyto a number, I wrote the code as follows. The Freecodecamp Debug console was not clearly displayed, could you please tell me where I made the mistake? Meanwhile, I have the following doubts: I have practiced HTML+CSS step by step, and now I am practicing JavaScript related content. However, I found that although I have done so much content, I still could not write the required code smoothly. Instead, I needed to retrieve various answers, but it seemed that I did not fully understand them. I will write code as a part of my future life, but I feel low efficiency. Where is my mistake and how can I become an excellent programmer? Please give me your valuable suggestions.


function findLongestWordLength(str) {
  let arrary = str.split();
 let longestvar = 0 ;
 for(let i = 0; i < arrary.length; i++){
   if(arrary[i].length > longestvar.length){
      longestvar.length = arrary[i].length
   }
 }
 return longestvar.length;
}

console.log(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/90.0.4430.93 Safari/537.36

Challenge: Find the Longest Word in a String

Link to the challenge:

First I would stop looking up answer and instead ask more questions on the forum when you are stuck. Looking at answers doesn’t help you solve future problems.

I’m taking a look at your code right now and I’ll let you know what I see.

@chucknorris The longestvar is not an array. It is an integer. So you can’t use .length method.

1 Like

Hint: Does a number have a length property?

Edit: sniped!

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I understand what you mean, numbers have no length, only array and string have length, I modified this line, but my Debug console shows 44, please help me to see what the problem is?

let longestvar = [];

Why did you make the number into an array?

You can’t directly change the length property of an array.

Thank you for your reply. I understand this and have not solved this problem yet :rofl:

So… Why did you switch to an array? (why is honestly the most important debugging question)

A number doesn’t have the length property, so why not change the code to not reference the length property?

I would also look into this line right here

If you look in the documentation, there is another way to right the split method where you can create an array of words like this

[ 'The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog' ]
2 Likes

I found that problem too. Thank you :blush: