2

I've been trying to capitalize the first letter of each word in a string, but it says TypeError: Cannot assign to read only property '0' of string 'i' . My logic looks fine but surely the way I'm doing this is not right. Any suggestions.

function titleCase(str) {
  str = str.toLowerCase();
  var word = str.split(" ");
  // console.log(word[0][0]);
  for (var i = 0; i < word.length - 1; i++) {
    word[i][0] = word[i][0].toUpperCase();
  }
  console.log(word);
  return word;
}

titleCase("I'm a little tea pot");
Thusitha
  • 3,393
  • 3
  • 21
  • 33
Ahsan Alii
  • 153
  • 1
  • 16
  • If you're not being paid by the character, try `s.replace(/\b\w/g,s=>s.toUpperCase())`. – RobG Sep 30 '17 at 08:29

6 Answers6

3

Try like so: (see comments in code)

function titleCase(str) {
  str=str.toLowerCase();
  var word = str.split(" ");
  for (var i=0; i < word.length; i++) {  // you don't need -1 here as you had
    word[i] = word[i].charAt(0).toUpperCase() + word[i].slice(1); // see changes in this line
  }
  console.log(word);
  return word;
}

titleCase("I'm a little tea pot");
caramba
  • 21,963
  • 19
  • 86
  • 127
2

Strings have a replace method that accepts a function:

var s = 'foo bar fum i am sparticus 23!!';

console.log(s.replace(/\b\w/g, function(s){return s.toUpperCase()}));
RobG
  • 142,382
  • 31
  • 172
  • 209
1

You can directly convert your string into what you want by inbuilt array function. Using map function you will get it directly no need to run for loop.

("I'm a little tea pot")
.split(" ")
.map(function(d){
 return d[0].toUpperCase()+d.substring(1,d.length)
}).join(" ")
Sanjay Kanani
  • 198
  • 13
1

You can combine split and map functions to achieve this.

function titleCase(str) {
  return str.toLowerCase().split(" ").map(function(word) {
    var _word = word.split("");
    _word[0] = _word[0].toUpperCase();
    return _word.join("");
  }).join(" ");
}

console.log(titleCase("I'm a little tea pot"));
Thusitha
  • 3,393
  • 3
  • 21
  • 33
1

With split, array map and reduce:

var str = "I'm a little tea pot";
var res = str.split(" ")
             .map(word => word.charAt(0).toUpperCase() + word.substr(1))
             .reduce((m, o) => { m = m + " " + o; return m }, "")
console.log(res);

Join can be also used instead of reduce:

    var str = "I'm a little tea pot";
    var res = str.split(" ")
                 .map(word => word.charAt(0).toUpperCase() + word.substr(1))
                 .join(" ");
    console.log(res);
Faly
  • 13,291
  • 2
  • 19
  • 37
1

You can use map function to create an array of modified words and join to recreate the string

function titleCase(str) {
  str = str.toLowerCase();
  var word = str.split(" ");

  var x = word.map(function(item) {
    return item.charAt(0).toUpperCase() + item.substring(1, item.length);
  }).join(" ");
  return x
}

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

Using only CSS

#test {
  text-transform: capitalize
}
<div id="test"> I'm a little tea pot</div>
brk
  • 48,835
  • 10
  • 56
  • 78