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()
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.
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!