0

function to capitalise first letter of a string - 'toUpperCase' , underscore and other jQuery are excluded . I reworked a vers with underscore which I can't use

```

function capitalize (str){
var str = "";
var lowercase = "";
var Uppercase = "";

str.forEach(){

for (i=0; i < str.length; i++);
}

return Uppercase[lowercase.indexOf(str0)];

}

```

There are lots of reduced vers using toUpperCase

Any links, code help pls .... Tks

  • The instance is specifically can't use touppercase . Is the point and question . – Voro_icles Feb 20 '16 at 01:46
  • Out of curiosity, why exclude specifically the right tool for the job? Is it a classroom assignment? – Álvaro González Feb 21 '16 at 17:50
  • @ÁlvaroGonzález the question is an exercise rather than a script edit. Exactly why I put the q on stack - exp. coders response - technicaly Unproductive to use other methods - no discussions on forums – Voro_icles Feb 21 '16 at 21:08

1 Answers1

1

The best method I've found is just to call toUpperCase on the first character and concat the rest of the string using slice:

function capitalize(str) {
  if(typeof str === 'string') {
    return str[0].toUpperCase() + str.slice(1);
  }
  return str;
}

If you want to capitalize each word in a sentence, you can split on space:

"capitalize each word of this sentence".split(' ').map(capitalize).join(' ');
Rob M.
  • 35,491
  • 6
  • 51
  • 50