Basic Algorithm Scripting - Title Case a Sentence

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:

First of all, it is a bad idea to mutate parameters. So,

str = str.toLowerCase();

is bad practice.

This:

str[i+1] = str[i+1].toUpperCase()

is bad JS.

Strings in JS are immutable, you can’t change individual characters like this. You can in some languages, but not JS. In JS, you have to create a whole new string. You can’t just change individual characters like you can change individual elements in an array, not in JS.

OK, i see. Thank you.
with things like .slice() or .splice() i thought strings are mutable. Maybe i’d read more documents to know how it actually works. And since i used things like str[i] and worked fine in previous challenges i thought it’s just like an array.
But I do wonder why it is a bad practice to modify input though. And it’s weird since str[i] worked, i did some superficial search on google and it says it can be treated as object or something. But in an object there is no order i guess? so is it something hybrid like array - object or how is it done?

If you google “mdn string slice”, it says:

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.

It doesn’t change the string.

And since i used things like str[i] and worked fine in previous challenges i thought it’s just like an array.

You can access individual characters that way, but you cannot change them.

But I do wonder why it is a bad practice to modify input though.

A function should not have unexpected side effects. I normally do not expect a function to mutate its input parameters. Granted, since it is a primitive, you can’t mutate it, but still, it is a bad habit to get into.

i did some superficial search on google and it says it can be treated as object or something.

I don’t know what the “it” is in that sentence and I don’t know how to respond to “or something”.

Strings are primitives in JS (not in other languages, like C). They are not objects. You can wrap them in an object wrapper, but that gets complicated and won’t get the result you want anyway.

But in an object there is no order i guess?

Typically, you don’t count on an order in an object, but I think JS actually guarantees it. But be careful, everything in JS that isn’t a primitive is an object - arrays, functions, etc.

so is it something hybrid like array - object or how is it done?

In JS, a string is a primitive. In some languages they are arrays or objects, or whatever. But in JS, it is a primitive and is immutable. How it works under the covers is not relevant - it is to be used as an immutable primitive. You cannot change it, just create a new version and save that.

2 Likes

OMG THANK YOU SO MUCH.
really appreciate so much effort. Got it now.