Title Case a Sentence5

Title Case a Sentence5


Tell us what’s happening:

Why its failing the test cases?

Your code so far

function titleCase(str) {
  let arr = str.split(' ');
  let narr= [];
  for(let i in arr){
narr[i] = arr[i].charAt(0).toUpperCase()+arr[i].slice(1).toLowerCase();
  return narr.join(''); 

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:


console.log() this and you might just find out your mistake.
Hint: It has something to do with this:

Try solving now.


Instead of using for, you can use filter(),
Also inside the filter callback: arr.filter(value => { // callback }) you can:

  • create a variable and assign the value with lowercase method: cont lowerString = value.toLowerCase()
  • Then you could create another variable and substract the first characther of the string and transform with upperCase method: lowerString.charAt(0).toUpperCase(). As you can see you have the first characther with upperCase.
  • And finally you can return this two variables with + but keep in mind that you have to slice the first variables and remove the first character and assign the new value. return varUpperCase + varLowerString.slice(1)


Another aproach could be with regular expression and replace method:

var string = 'YAMIT VILLAMIL';
var regExp = /([a-zA-Z])([a-zA-Z]+)/g;

function caseSensitive(match, group1, group2) {
     return group1.toUpperCase() + group2.toLowerCase();

string = string.replace( regExp, caseSensitive);