0

Code:

function titleCase(str) {
let a = str.concat();
a = a.toLowerCase().split(" ");

let v = a.map(function(item){
item[0] = item[0].toUpperCase();
return item;
});
 
return v.join(" ");
}

console.log(titleCase("I'm a little tea pot"));//output should be "I'm A Little Tea Pot" but shows "i'm a little tea pot"

The code should simply show the output as indicated in the comment on the last line. The value of item[0] is not changing.

4 Answers4

2

You can do this pretty easy and fast with a little regex

function titleCase(str) {
  return str.replace(/\s(\w)|^(\w)/g, e => e.toUpperCase())
}

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

Your code doesn't work because a string is immutable, meaning you can't assign a new value to it like that. If you want to keep your loop, you can use below instead

function titleCase(str) {
  return str.split(' ').map(function(item){
    item = item.substring(0,1).toUpperCase() + item.substring(1);
    return item;
  }).join(' ');
}

console.log(titleCase("I'm a little tea pot"));
baao
  • 67,185
  • 15
  • 124
  • 181
  • At first, I did use regex to change the case of the sentence which worked. Then, I tried to change the case using the `map()` function to alter the content of the string which didn't actually work. Your code worked and I actually used `return item[0].toUpperCase() + item.slice(1)`. – Prakhar Saxena Jun 14 '18 at 19:16
1

You can also correct your method like following

function titleCase(str) {
  return str.toLowerCase().split(" ").map(s => s[0].toUpperCase() + s.slice(1)).join(" ");
}

console.log(titleCase("I'm a little tea pot"));
Nikhil Aggarwal
  • 27,657
  • 4
  • 40
  • 56
0

item is a string. Strings are immutable. In particular, you cannot assign to individual characters to change the string. That's why item[0] = ... has no effect.

There are a couple of ways you could do this instead.

Build a new string instead of trying to modify item in place:

function titleCase(str) {
  const a = str.toLowerCase().split(" ");

  const v = a.map(function(item) {
    return item[0].toUpperCase() + item.substr(1);
  });

  return v.join(" ");
}

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

Or just use a regex search/replace:

function titleCase(str) {
  return str.replace(/(^|\s)(\w)/g, function(m0, m1, m2) {
    return m1 + m2.toUpperCase();
  });
}

console.log(titleCase("I'm a little tea pot"));
melpomene
  • 81,915
  • 7
  • 76
  • 137
0

Add a few console.log() to debug your code along the way. It'll help you see how your string transforms.

   function titleCase(str) {
      // let a = str.concat(); // No need to concat a string.
      let words = str.toLowerCase().split(" ");

      // item is individual words
      let v = words.map(function(item){
        let title = item.charAt(0).toUpperCase() + item.slice(1) // You need to select letters by charAt(...)
        return title;
      });

      return v.join(" ");
    }

    console.log(titleCase("I'm a little tea pot"));
Dan
  • 1,853
  • 2
  • 12
  • 41