0
function makeTitle(str) {
let arr = str.split(' ');

  for(let i=0; i<arr.length; i++) {
    arr[i][0] = arr[i][0].toUpperCase();
  }
  return arr;
}

makeTitle("This is a title") 
  //➞ "This Is A Title"

Hey guys, why isn't this changing the first letter of every word in the array? When I console.log it, I can see it selects 'T' 'i' 'a' and 't' like its supposed to. So why doesn't it change their values?

Brixsta
  • 605
  • 12
  • 24

1 Answers1

0

You cannot change a string directly once it's set. Instead you can create a new string and return it. So to get this to work properly, you would get the letter change it, and then get the remaining string and join them. Then replace the value in the array. Something like this.

arr[i] = arr[i][0].toUpperCase() + arr[i].slice(1)
Descifle
  • 24
  • 3