1

I want to capitalize the first letter of every word in a string.

I want to know why this is not working:

function titleCase(str) {
  str = str.toLowerCase().split(" ");
  for(let i = 0; i<ar.lrngth;i++){
    ar[i][0] = ar[i][0].toUpperCase();
  }
  return ar;
}

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

Nor this:

function titleCase(str) {
  str = str.toLowerCase().split(" ");
  let _ar = [];
  _ar =str.map(item => item.map( cap => cap.toUpperCase()));
  return _ar;
}

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

I would like to know if there are any one liners that can do this too : same operation on elements of subarrays

Majd
  • 328
  • 4
  • 12
  • Does this answer your question? [How can I capitalize the first letter of each word in a string using JavaScript?](https://stackoverflow.com/questions/32589197/how-can-i-capitalize-the-first-letter-of-each-word-in-a-string-using-javascript) – Majed Badawi Mar 07 '21 at 13:49
  • @MajedBadawi yes it does solve the problem, i want to know wh my code isn't working – Majd Mar 07 '21 at 13:52

5 Answers5

1

Here is good one line answer with es6 style - https://stackoverflow.com/a/43376967/15348051

Additionally, I Think the reason why it is not working is your code try to change Primitive value str[i][0] not reference type variable str's array element.

for(let i = 0; i<ar.lrngth;i++){
    str[i][0] = str[i][0].toUpperCase(); // this one
}

you try like this

let aa = "i'm"
aa[0] = "I" // #2
console.log(aa) // "i'm"

this is not working, because variable aa is assigned primitive type string "i'm". So when you assign it like #2 in the code, you tried to change primative type not the variable aa

this is reference type example.

const aa = ["i","'","m"]
aa[0] = "I"
console.log(aa) // ["I","'","m"]

It works. Because it tried to change the array's element. array and object can change their elements by index or key.

I hope I explain it right :)

0

See if that works:

  function capitalize(text) {
    let str = text.toLowerCase();
    str = str.replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase());

  ;    return str;
  };
Rodrigo V
  • 434
  • 1
  • 7
  • 14
0

Strings are immutable in JavaScript and it is not an array of characters.

const str = "Test";

str[0] = "K";

console.log(str);

It won't change the string as you can see in the above example.

When we use bracket notation for character access, we cannot delete or assign a new value. If we do, it silently fails. mdn

So we need to assign a new string with the uppercase first character and rest as it is.

What you can also do is. Split array into a character array and uppercase the first character and join them. But here still we are creating a new array with split and after joining the array we get the new string.

let str = "test string";

str = str.split("");
str[0] = str[0].toUpperCase();
str = str.join("");
console.log(str);

function titleCase(str) {
  str = str.toLowerCase().split(" ");
  console.log(str);
  for (let i = 0; i < str.length; i++) {
    str[i] = str[i][0].toUpperCase() + str[i].slice(1);
  }
  return str.join(" ");
}

console.log(titleCase("I'm a little tea pot"));
DecPK
  • 24,537
  • 6
  • 26
  • 42
0

Here is another way to capitalize all words in a sentence.

  const titleCase = (str) => {
      let initValue = str[0].toUpperCase();
      for (let i = 1; i < str.length; i++) {
        if (str[i - 1] === " ") {
          initValue += str[i].toUpperCase();
        } else {
          initValue += str[i];
        }
      }
      return initValue;
    };

    const result = titleCase("I'm a little tea pot");
     console.log(result);
FD3
  • 1,462
  • 6
  • 24
  • 47
0

In your code, ar is not defined. Also for(let i = 0; i<ar.lrngth;i++){ lrngth should be length. If you define ar as var ar = [] still it not work at ar[i][0] = ar[i][0].toUpperCase(); since there was no element in ar[i][0] to uppercase. To work your code, it can be modified as follows,

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

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

This way also works.

function titleCase(str) {
  console.log(str);
  
  str = str.toLowerCase().split(" ");
  
  for(let i = 0; i<str.length; i++){
    str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1);
  }
  
  return str.join(" ");
}

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