Title Case a Sentence:

Hello,
Title Case a Sentence
in this code:

function titleCase(str) {
  let minStr = str.toLowerCase().split(' ');
  console.log(minStr)
 let result = [];
  for (let i=0 ; i < minStr.length; i++) {
    let tabWord = minStr[i].split('').splice(0,1,minStr[i][0].toUpperCase());
  
    console.log(tabWord)
   
  }
  //console.log(minStr)
}

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

the result is:
image

and if I do that:

let tabWord = minStr[i].split('')
    tabWord.splice(0,1,minStr[i][0].toUpperCase());

the result is: it’s what I need
image

and if I do that:

let tabWord = minStr[i].split('')
let tabUpper = tabWord.splice(0,1,minStr[i][0].toUpperCase());
    console.log(tabUpper)

image

Whyyy this differences?

So I come back to the second step and I add join():

let tabWord = minStr[i].split('')
    tabWord.splice(0,1,minStr[i][0].toUpperCase()).join('');

this change anything:
image

but if I do that:

let tabWord = minStr[i].split('')
     tabWord.splice(0,1,minStr[i][0].toUpperCase());
    let stringUpper = tabWord.join('')

    console.log(stringUpper)

That’s works !!!
image

Whyyyy? :laughing:
Could you please say to me what mistakes of thought I make in each case ?

I understood in the first case.
Could you just explain to me why join() doesn’t work in this case ?

let tabWord = minStr[i].split('')
    tabWord.splice(0,1,minStr[i][0].toUpperCase()).join('');

It looks like its because you only have the one letter that you are capitalizing in there. Cant really join anything if its just the one letter.

However at this step:

Each words are well broken down into a table with each letter. It’s not just one letter. :roll_eyes:

Join makes a new string. Where are you putting the new string? It looks like you are just abandoning the new string?

console.log(tabWord.splice(0,0,minStr[i][0].toUpperCase()).join('')) gave me the only the lower case letter. i then did a little reading on splice and found:

Return value

An array containing the deleted elements.

If only one element is removed, an array of one element is returned.

If no elements are removed, an empty array is returned.

so there is only one letter, and its the lowercase letter being deleted.

2 Likes

Ah, good catch. Method chaining works on the return values.

Hi Jeremy, my pleasure to work with you again.
It’s a problem that I hadn’t well understood in last september. Today I rework this execice and I wonder the same question.
So I hope I will finally well understans.

It’s weird for me because with let tabWord = minStr[i].split('') we transform a string to a table. So it’s weird that it’s not possible to make the reverse. Table to string.

I try to understand you last posts…

Notice you are saving the new array in a variable

It all comes down to two issues

  1. Splice returns the values that were removed. You can’t call join after a splice and get the remaining letters all joined up… With splice.join you get the removed letters joined up.

  2. Join makes a new string. That new string needs to be saved somewhere.

Ok I well understand for splice. So for sum up , can I say that it’s due to the splice methode ?
because when I look the FCC solution 2:

function titleCase(str) {
  return str
    .toLowerCase()
    .split(" ")
    .map(val => val.replace(val.charAt(0), val.charAt(0).toUpperCase()))
    .join(" ");
}

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

here the methodes’ actions are put one behind the other and as map just tranforme the string it’s possible to use join at the end.
I don’t know if I have rigth to make this comparing because in this case str still a string from begining to the end. :thinking:

This one goes string → string → array → array → string

yes you are rigth. So it’s just due to splice methode.
so do think I well all understood now ?

Yeah, this chaining can be tricky but it seems you understand the chaining issues in thus challenge now!

1 Like

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