Title Case a Sentence (1)

Tell us what’s happening:
Describe your issue in detail here.

Doing the short capitalization challenge, pretty sure I have the right results but it says I dont.

  **Your code so far**

function titleCase(str) {
let str2 = []
str2 = str.split(' ');
str = ''
for(let i = 0; i < str2.length; i++){
  str += (str2[i].substr(0,1).toUpperCase()).concat(str2[i].substr(1).toLowerCase() + ' ');
return str;

//titleCase("I'm a little tea pot");
console.log(titleCase("sHoRt AnD sToUt"));
console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"))
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0

Challenge: Title Case a Sentence

Link to the challenge:

this doesn’t mutate the string

I’ve been recently advised to make more use of new variables when making assignment (=) so to use ‘const’ more than ‘let’.
(i’m just a noob, so pinch of salt with my advice, but i think you could apply that here). some functions mutate a value (could be best avoided anyway) and some return a new thingumeejig. )

I see, I didnt realize that. I just set the return value to str.trim() and it worked, thx for the heads up.

const str2=str.trim() wud also work

I wouldn’t overwrite the input like this.

Why not use

const str2 = str.split(' ');

This really isn’t a great variable name.

I’d format it more like this for readability:

str += str2[i][0]
  .concat(str2[i].substr(1).toLowerCase()) + ' ';
1 Like

Yeah it would definitely work.

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