Title Case a Sentence - read-only error

Hello,

I am getting an error “4 is read-only” when running the below code. Can anyone help and advise why i am getting the error?

Thanks


function titleCase(str) {
for (var i = 0; i < str.length; i++) {
 var n = i - 1;
 if (str[n] === " "){
 str[i] = str[i].toUpperCase();   
 }else{
   str[i] = str[i].toLowerCase()
 }
}
  return str;
}


titleCase("I'm a little tea pot");

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

Strings are immutable, you can’t modify them in-place. This:

str[i] = str[i].toLowerCase()

You can’t do that.