Tell us what’s happening:
TypeError: Cannot assign to read only property ‘1’ of string ’ i’m a little tea pot’
this TypeError is just annoying, why this happens and how should i fix? i mean literally on both sides there are “string[i+1]”. On the right side is what i want, treating the string like an array or sth, but on the left side the warning tells me it interprets it as an Object. DUDE WHY ISN’T IT POSSIBLE?
to simplify, i mean str = str[i] is completely fine but why won’t str[i] = str[i] work?
Your code so far
function titleCase(str) {
str = str.toLowerCase();
str = ' ' + str;
for (let i = 0; i < str.length; i++){
if (str[i]==' '){
str[i+1] = str[i+1].toUpperCase()
}
}
str = str.slice(1);
return str;
}
titleCase("I'm a little tea pot");
Challenge: Basic Algorithm Scripting - Title Case a Sentence
Link to the challenge: