Returning first letter of index in array to uppercase. What am I doing wrong here?

Beginner here working my way through the ‘Basic Algorithm Scripting’ section of FCC. This question relates to ‘Basic Algorithm Scripting: Title Case a Sentence’ but indirectly.

Would someone please explain to me my misunderstanding here?

I would expect the following code to produce arr = [‘Red’,‘blue’,‘green’]

let arr = [‘red’,‘blue’,‘green’];
arr[0][0] = arr[0][0].toUpperCase();

console.log(arr);

But this outputs: ‘red’,‘blue’,‘green’

I have a feeling it has something to do with strings vs. arrays but i’m not really sure.

Also what is the code that would produce arr = [‘Red’,‘blue’,‘green’]?

Thanks in advance!

Strings are immutable, you can’t mutate characters in the string. You would normally take your string, copy the first letter and uppercase it, and join that to a copy of the rest of the string.

1 Like

Ahh I see! After some figuring I was finally able to pass ‘Basic Algorithm Scripting: Title Case a Sentence’ with your advice! Thanks DanCouper!

1 Like