Title Case a Sentence: callback function

Title Case a Sentence: callback function
0

Keep going long enough and you’ll get to know the ins and outs of prototypes. This is one of the many ways that careful naming of variables can save you a headache. If you use something like lowerCaseArr instead of x, you’ll remember that it is an array and that you have array methods (and only array methods) available to you.

1 Like

Ariel. I’ve been working out. I couldn’t get the solution. But I have this:

function titleCase(str) {
  let myArr = str.split(" ");
  let arr1 = [];
  let arr2 = [];
  let arr3 = [];
  let str1;
  let str2;
  
  
  for (let i = 0; i < myArr.length; i++){
    arr1.push(myArr[i].toLowerCase());
    arr2.push(arr1[i].charAt(0).toUpperCase());
    arr3.push(arr2[i] + arr1[i]);
  }  
  
  let index = 1;
  for (let j = 0; j < arr3.length; j++){
    str2 = arr3[j].slice(0,index).concat(arr3[j].slice(index+1))
  }

   return str2;//--> "Stout"
}

titleCase("sHoRt AnD sToUt");

Problem: I don’t understand why It only works at the third word. As far as I understand str2 is a string. Is it a problem with the structure of the second loop statment? What am I missing?

str2 is going to contain only the last word because your second loop is always overwriting it with the current word.

1 Like

…because str2 it is not an array, right? I hope I got it.

Ariel, now you can sleep. Finally, I got one solution:

function titleCase(str) {
    let myArr = str.split(" ");
    let arr1 = [];
    let arr2 = [];
    let arr3 = [];
    let arr4 = [];
    let str1;
    
    
    for (let i = 0; i < myArr.length; i++){
      arr1.push(myArr[i].toLowerCase());
      arr2.push(arr1[i].charAt(0).toUpperCase());
      arr3.push(arr2[i] + arr1[i]);
    }  
    
    for (let i = 0; i < arr3.length; i++) {
      str1 = arr3[i].slice(0,1).concat(arr3[i].slice(2));
      arr4.push(str1);   
    }
    
    let result = arr4.join(" "); 
    
    return result;     
  }

Good job finding a solution. Now give your poor brain a rest :smiley:

My brain is just warming up:

function titleCase(str) {
    const arr3 = str.split(" ");
    const arr4 = arr3.map(str => str.toUpperCase().slice(0, 1).concat(str.toLocaleLowerCase().slice(1)));

    return arr4.join(" ");
}

titleCase("I'm a little tea pot");

It seems that as I lost the pressure of solving it, it got easy to think over in easier ways.
By the way, thanks for you feedback.

Just a few helpful suggestions:

  1. You don’t really need the const fooStr = str; line. You can just split str and assign to arr3 (though I would use a more meaningful variable name like wordsArr).

  2. The following line does not really make sense, because the join method creates a string and not an array (as you reference with arr5).

let arr5 = arr4.join(" ");

To shorten the code a bit, you could simply write:

return arr4.join(" ");

To help others and yourself when you go back to review older code, try to get in the habit of creating variable names which make it instantly clear what data they reference.

1 Like

Got it. Thanks for the feedback!