'how do i that? I want the first character of the string to be uppercase and the rest lowercase [duplicate]

I want the first character of the string to be uppercase and the rest lowercase this is what I code

let title = prompt().toLowerCase();
for (let c of title)
{
    c[0]=c[0].toUpperCase();
}


Solution 1:[1]

  • There are many methods, and the following is just one of them.
    function toUpperCaseFirstLetter(str) {
      if (typeof str !== 'string' || str.length === 0) {
        return str;
      }

      return str[0].toUpperCase() + str.slice(1).toLowerCase();
    }

    // some tests
    expect(toUpperCaseFirstLetter('abc')).eql('Abc');
    expect(toUpperCaseFirstLetter('ABC')).eql('Abc');
    expect(toUpperCaseFirstLetter('')).eql('');
    // other tests....

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 skypesky