0

I have converted the string into an array and want to replace every first word with a capital letter for that I am using the replace method but not working

function titleCase(str) {
  let arr = str.toLowerCase().split(' ');
  for (let i = 0; i < arr.length; i++) {
    arr[i].replace(arr[i][0], arr[i][0].toUpperCase())
  }
  
  let a = arr.join(' ');
  return a;
}

console.log(titleCase("I'm a little tea pot"));
adiga
  • 34,372
  • 9
  • 61
  • 83
  • 2
    The `.replace()` function does not modify the original string; it returns a *new* string. – Pointy Sep 01 '19 at 12:29
  • Thanks a lot, I changed the code and it worked function titleCase(str) { let arr= str.toLowerCase().split(' '); console.log(arr); let narr=[]; for(let i =0;i – Aditya Lamba Sep 01 '19 at 12:33

1 Answers1

0

You just need that

function titleCase(str) {
  let arr = str.toLowerCase().split(' ');
  var str = []
  for (let i = 0; i < arr.length; i++) {
    str[i] = arr[i].replace(arr[i][0], arr[i][0].toUpperCase());
  }
 
  return str.join(' ');
}

console.log(titleCase("I'm a little tea pot"));
ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36