Title Case a Sentence: callback function

Title Case a Sentence: callback function
0

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

2 Likes

Yes it does. So if I replace the original first chart with an upperCased char coming from another var, it would work?

Ok I’ll try. (I didn’t see the spoiled code)


1 Like

Got it. Look I have made some progress. Now I turned each first letter into upperCase. BUT 1) do I need to stored out from the loop? 2) how to replace the first letter of x?

here my try

function titleCase(str) {
  let myArr = str.split(" ");
  let x = [];
  let charTemp;
  
  for (let i = 0; i < myArr.length; i++){
    x.push(myArr[i].toLowerCase());
    charTemp = x[i].charAt(0).toUpperCase();//return S A S
    let y = x[i].replace(charAt(0), "w")//this stops the loop, I'm using the "w" just for trying.
    
    console.log(charTemp);// prompts S(1st block), A(2nd), etc.  
    console.log(x);
    console.log(y);
  }
  
  
 

   return x;
}



titleCase("sHoRt AnD sToUt");

There are different ways that you could replace the first letter of each word. You could use replace() or you could add together two substrings (the capital first letter and the lower-case rest of the word), just to name the most straightforward approaches.

I have this. If I take the second loop out, prints “Sshort,Aand,Sstout”. I couldn’t append splice to anything in the first loop, so I implemeted the second one…

function titleCase(str) {
  let myArr = str.split(" ");
  let x = [];
  let charTemp = [];
  let y = [];
  let z = [];
  
  
  for (let i = 0; i < myArr.length; i++){
    x.push(myArr[i].toLowerCase());    
    charTemp.push(x[i].charAt(0).toUpperCase());
    x[i].shift;
    y.push(charTemp[i] + x[i]);
  }
  
  for (let j = 0; j < y.length; j++){
    for (let k= 0; k < y[j].length; k++){
      y[j][k].splice(1,1);
    }
  }
   console.log(y);


   return y;
}



titleCase("sHoRt AnD sToUt");

y[j][k] is not an array, so it won’t support the splice method. Keep chipping away it it! I know that this can feel frustrating, but struggling through a challenge like this really helps you understand what you’re doing.

Well… I have this:

function upperChar (str) {
    return str.toUpperCase();
}

let a = "bernardo";
let b = a.replace(a.charAt(0), upperChar);
console.log(b) // --> Bernardo

It works. BUT, when I try to apply to the challenge: nothing…

function titleCase(str) {
  let myArr = str.split(" ");
  let x = [];
  let charTemp = [];
  let y = [];
  let a = [];
  
  
  for (let i = 0; i < myArr.length; i++){
    x.push(myArr[i].toLowerCase());
    a = x.replace(x.charAt(0), upperChar);//OR   a = x.replace(x[i].charAt(0), upperChar);
    console.log(a);
    charTemp.push(x[i].charAt(0).toUpperCase());
    y.push(charTemp[i] + x[i]);
  }

  function upperChar (str) {
    return str.toUpperCase();
}

   return a;
}


titleCase("sHoRt AnD sToUt");

Well… I have this:
by the way: I really hate for loops

function upperChar (str) {
    return str.toUpperCase();
}

let a = "bernardo";
let b = a.replace(a.charAt(0), upperChar);
console.log(b) // --> Bernardo

It works. BUT, when I try to apply to the challenge: nothing…

function titleCase(str) {
  let myArr = str.split(" ");
  let x = [];
  let charTemp = [];
  let y = [];
  let a = [];
  
  
  for (let i = 0; i < myArr.length; i++){
    x.push(myArr[i].toLowerCase());
    a = x.replace(x.charAt(0), upperChar);//OR   a = x.replace(x[i].charAt(0), upperChar);
    console.log(a);
    charTemp.push(x[i].charAt(0).toUpperCase());
    y.push(charTemp[i] + x[i]);
  }

  function upperChar (str) {
    return str.toUpperCase();
}

   return a;
}


titleCase("sHoRt AnD sToUt");

Please do not create multiple topics for the same conversation. I have merged your topics.

x is an array. It does not have a charAt method or a replace method.

Oh, it was a way to, you know, retake our conversation. Anyways, thanks for the feedback. Now I finally understand that objects has specific properties and methods so that’s why replace wasn’t working in my previous tries…

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.