Title Case a Sentence: callback function

Title Case a Sentence: callback function
0

Tell us what’s happening:
Guys, I haven’t figure out how to turn the first letter into upper case. Any hints to do it?
And, also, I want to script a callback function firstLetterToUpper, in order to use replace…

Your code so far


function titleCase(str) {
  let myArr = str.split(" ");
  //console.log(myArr[0]);
  let x = [];
  
  for (let i = 0; i < myArr.length; i++){
    x.push(myArr[i].toLowerCase());   
  }
  console.log(x);//--> short,and,stout
  
 // x.replace(x[0][0], firstLetterToUpper()); //trying to replace first letter of each array part

   return x;
}

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

console.log(titleCase("sHoRt AnD sToUt"));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.75.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence/

Remember that you can access the characters in a string by index.

Exactly. But I missing something, because what I’m trying with x[0][0] is gain access to the 0 index of the 0 word (i. e. “s”, from “short”).

I think that you may want to look in more depth at how replace() works.

As an aside, is there a particular reason that you want to create a function for calling toUpperCase()?

Yeah. I read the mozilla replace and, as long as I understand, they say that it takes as arguments strings and functions. The function would be intended to turns chars into upperCase, repeatedly.

Does replace modify the string that it is called on?

  1. Put every member from split array in temporary variable.
  2. After that, first char of said variable turn to lowercase.
  3. Then temp var push into array.
for (let i = 0; i < myArr.length; i++){ 
    let tmp = myArr[i];
    tmp = tmp[0].toLowerCase();
    x.push(tmp.toLowerCase()); 
}

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…