Find the longest word in a string - cannot assing the read only property 'length' of string

Find the longest word in a string - cannot assing the read only property 'length' of string
0

#1

Hi guys, I am currently at this challenge and I feel like I’ve got the idea of how to complete it but there’s a little problem.

Whenever I run this code I get a TypeError {message: “Cannot assign to read only property ‘length’ of string ‘The’”}. Is there a workaround for this? What is the reason for this?

Here’s my code:

function findLongestWord(str) {
  
  var arrayedString = str.split(" ");
  var lengthOfLongestString = 0;
  
  for(var i = 0; i < arrayedString.length; i++) {
    if(arrayedString[i].length > lengthOfLongestString) {
      arrayedString[i].length = lengthOfLongestString;
    }
  }
  
  return lengthOfLongestString;
}

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

Any help would be appreciated!


#2

Just as it says. The .length property of strings is read-only (you can’t assign them values like you do with variables).

Look carefully here: arrayedString[i].length = lengthOfLongestString;. Your code’s error is right at this line.


#3

Oh God I just had to turn them the other way round. :smiley:

That’s why I have Daredevil as my avatar I guess. Thanks!