It is necessary to replace all last names starting with a small letter with the same last names that only begin with a capital title, i.e. change the case of the first letter. Where is the mistake?
<script>

var a=["бердяев","Афанасенко","савельев"]; document.write("Input:<br>"); document.write(a+"<br><br>"); for (var i=0;i<a.length;i++) { if ((a[i].charAt(0))==['а-я']) 

`` {a [i] = a [i] .charAt (0) .toUpperCase () + a [i] .substr (1) .toLowerCase ();} else continue; } document.write ("Output: <br>" + a);

 </script> 
  • What does a[i].charAt(0))==['az'] mean? (1) "Berdyaev" - the first character - a space - alexlz
  • Removed spaces, and corrected ['az'] to ['a-z'], because Russian letters. In the end, still refuses to translate into upper case. - GTRL
  • That is, compare the first character with the string '[AZ]'. And they do not match. What a trouble. Maybe you wanted /a./.test(aai a.charAt(0)) (not the most suitable option, since you can also replace the register with regular expressions) - alexlz
  • And what's the point of checking? If you know that there are only names in the array, then why not just translate all the first letters in upper case? It also does not hurt to remove spaces. In general, something like this - Deonis
  • Thought somehow you can do without regular expressions. But, thank you all for your valuable answers. - GTRL

2 answers 2

 String.prototype.ucfirst = function() { var str = this; if(str.length) { str = str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(); } return str; }; alert( 'LALAlala'.ucfirst() ); 
  • And how to adapt it for the case of my array? Need to view items in a loop? - GTRL
  • You can use your hands in a loop, and you can do this: Array.prototype.ucfirst = function () {var a = this; for (i = 0; i <a.length; i ++) {// type checks a [i] no: a [i] = a [i] .ucfirst (); } return a; } alert (['LALAlala', 'ZaZaZa']. ucfirst ()); - user6550
  • This code is not executed. Or did I forget something? jsfiddle.net/vxraC/1 - GTRL
  • Of course, it does not work - where did String.prototype.ucfirst () go? - user6550
  • Yes, everything corrected, thanks. - GTRL
 function titleCase(str) { var arr = str.toLowerCase().split(' '); var result = arr.map(function(val){ return val.replace(val.charAt(0), val.charAt(0).toUpperCase()); }); return result.join(' '); } titleCase("I'm a little tea pot"); 

Leave it here. It may be useful to someone)

  • one
    You will undoubtedly improve the quality of the answer, if instead of a deprived information of the epilogue you add a couple of explanations to your code snippet. - 0xdb