Trouble with challenge "Title Case a Sentence"


I’m performing the Title Case a Sentence challengeof the Basic Algorithm Scripting and I’m having some trouble.
The code has many lines, but I’m not concerned with that right now.

I have created an array that each item is a part of the title. The problem is that I need to remove the first letter of each item and, then, add the first letter that is part of a variable (upperInitials), but I don’t know how to do that. I’ve looked in many places, but I can’t find a solution.

How to do that?

I add comments to the code to show my steps.


function titleCase(str) {
  let myRegex = /(\w+'|\w)\w*( |\w+)/gi;
  let words = str.match(myRegex);    // Extract each word (+ space) of the string
  // console.log(words);

let initials = [];
  for (let j = 0; j < words.length; j++) {
    if (j < words.length) {
      initials.push(words[j][0]);   // Extract the initial of each word
    // console.log(initials);

  let upperStrInitials = initials.toLocaleString(); // Creat a string w/ initials
  // console.log(upperStrInitials);
  let upperInitials = upperStrInitials.toUpperCase(); // Upper Case initials
  // console.log(upperInitials);
  // console.log(words);
  for (let l = 0; l < words.length; l++) {  // How to remove (shift?) the first character of each item? How to add (unshift?) a character to the beginning of each item? Should I put each item into an array? And how to do that?
  let reunited = "";

  for (let i = 0; i < words.length; i++) {
    reunited = reunited.concat(words[i]);     // Re-create the original string
// console.log(reunited);

  return reunited;

titleCase("I'm a little tea pot");
// console.log(titleCase("I'm a little tea pot"));

Hey there,

can you describe in simple english comments,
what your current algorithm should do step by step?

1 Like

Hi @miku86!

function titleCase(str) {
  let myRegex = /(\w+'|\w)\w*( |\w+)/gi;
  let words = str.match(myRegex); 

It must “transform” the string “I’m a little tea pot” into an array [“I’m”, “a”, “little”, “tea”, “pot”], with each word (plus one space, except for the last word) being an element. I did this to be able to deal with each word separately.

let initials = [];
  for (let j = 0; j < words.length; j++) {
    if (j < words.length) {

It must create an array with each element being the first letter of each word (here, I am considering “I’m” as a single word). I did this to be able to deal with each initial separately.

let upperStrInitials = initials.toLocaleString();
let upperInitials = upperStrInitials.toUpperCase(); 

It must (first) transform the array containing each initial into a string and (second) capitalize each letter in the string. I did this in order to capitalize each letter. I thought about using each of these letters to replace the first letter of each word with the corresponding index.

And now it 's the part I’m stuck with. I don’t know how to replace the initial letters of each word with the capital letters that are in the “upperInitials” variable.

The rest of the code should take the elements of the array with the words starting with a capital letter and return a string (the sentence “I’m A Little Tea Pot”).


So why don’t you take the upperInitial for one word and add the remaining characters and save/return this as a new word?

This would be something like:

// loop through each word
  // uppercase first character of word
  // add remaining characters of word
  // save/return new word
// take all new words and join them

This seems more intuitive to me.

The map function would be really handy for this.

1 Like

Thanks @miku86!
I’ll try this and see how it works.
Thank you very much!!

I was not able to understand very well how the .map () method works. I will try to understand better. I need to review the basics. I realized that my knowledge of how a function works is minimal. I started doing another one and went to a part. I ended up looking at the solutions and copied the solution I didn’t know how to do from solution 2. I saw and understood, but I don’t know if I can elaborate such a function.
Anyway, I needed to finish this, because it was draining all my energy.
Although not ideal, I noticed several of my flaws. And I realized that I went too fast on the basics of the basics.

Anyway, that’s what I did.

function titleCase(str) {
  let lowerCase = str.toLowerCase();

  let myRegex = /(\w+'|\w)\w*( |\w+)/gi;
  let words = lowerCase.match(myRegex);
  var result = {
    return val.replace(val.charAt(0), val.charAt(0).toUpperCase());
  return result.join("");

titleCase("I'm a little tea pot");
function titleCase(str) {
  //I create an empty string to save values to latter

  //Importantly you will noticed I do not bother making an array as this is not
  //always needed and makes your code just a tiny bit slower

  //I also avoid using regex because while powerful, it also make things slow
  //and should only be used where it's really needed

  let s = "";

  //I loop through the length of the string passing all characters 
  for (let i = 0; i < str.length; i++) {
    //I reassign my s variable using conditionals to make the correct 
    if (i === 0 || str[i - 1] === " ") {
      s += str[i].toUpperCase();
    s += str[i].toLowerCase()
  //I now have the correct value so I return it
  return s;

titleCase("sHoRt AnD sToUt");
1 Like

Hi @caryaharper!!
I appreciate your code with the comments explaining your steps.
Very simple. Great code!
Thank you very much!! =D