Title case sentence question

function titleCase(str) {

  let temp=0;

for (let i=0;i<str.length;i++){

  if (str[i] ===" "){

    i +=1;

    temp = str.charCodeAt(str[i]);

    temp +=32;

    str[i]=String.fromCharCode(temp);

  }

}

return str;

}

titleCase("I'm a little tea pot");
// TypeError: Cannot assign to read only property '4' of string 'I'm a little tea pot'

Strings are “read only”, meaning you cannot override any value within them.
You can create a new string and tell JS to override the entire thing → meaning you tell it to assign the variable-name to a completly new object.
But you cannot string[i] = whatever.

Pro tip: It’s easier for us to help if you write your question rather than just posting code :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.