# Find the Longest Word in a String - Basic Algorithm Scripting Question

Tell us what’s happening:
I am trying to solve this challenge with the code below. But I am getting an error stating: Cannot assign to read only property ‘0’ of string ‘The quick brown fox jumped over the lazy dog’
What does it mean?
How can I solve it?
How can I improve my code?
And please give suggestions for my code as I’m just a beginner and trying to learn things on my own.

Your code so far

``````
function findLongestWordLength(str) {
var count = 0;
var arr = [];
for (var i = 0; i <= str.length-1; i++) {
count++;
if (str[i] = " ") {
arr.push(count);
count = 0;
}
}
return Math.max(arr);
}

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/75.0.3770.100 Safari/537.36`.

With this you are trying to change the value of a character of the string. Remember the difference between `=`, assignment operator, and `==` or `===` comparison operators

Also think about what type of value you are trying to convert with `Math.max` If you’re unsure what value you’re trying to convert try `typeof(value)` . It will tell you what type of value your variable is.

You probably already know, but you’ll if you’re debugging need to print that to the console, so use `console.log()`

Keep it up! You can do this

1 Like

Okay. I’ll change it.
Thanks a lot!

Sure.
I am thinking of applying `Math.max.apply(null, arr)`. I think will do.
Thanks a lot!

1 Like

No worries

How did you go?

It didn’t work.
I don’t know why.
Maybe, my code is wrong. I am checking it again now.

I think you can do it like the following.

`<redacted>`

Okay. I’ll try it.
Thanks.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

2 Likes

Also don’t worry too much if you’re struggling to find the correct answer to a coding question. Coding can be hard but it’s normal to struggle. That’s the process of learning. The forums are designed for helping each other out. So don’t be afraid to ask questions and also take breaks from your computer and give yourself a pat on the back for your hard work. You can do it!

Sure. Thanks for your kind words.

1 Like

I have modified the code as follows:

function findLongestWordLength(str) {
var count = 0;
var arr = [];
for (var i = 0; i <= str.length-1; i++) {
count += 1;
if (str[i] === " ") {
arr.push(count);
count = 0;
}
}
var val = arr[0];
for(var j = 0; j <= arr.length-1; j++) {
if(arr[j] > val) {
val = arr[j];
}
}
return val;

}

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

I am still not passing the test cases.
Please tell me what else do I need to change in this code?

I don’t have the time to find where the issue is, but you have an out-by-one error, as in, your function returns `7` but the longest word has length `6`

Also, your code can’t work if the longest word is the last in the string as your take the length of a word only when it reaches a space

Oh, wait. You incrememnt length also on a space, as first you have `count += 1;` and then the check if it is a space

Okay. I’ll commit the changes.