function ucFirst(str) { var b = str.substring(1); var a = str.charAt().toUpperCase(); console.log(a + b); } ucFirst('вася'); 

Write the function ucFirst(str) , which returns the string str with the first capital character, for example: "Vasya" ;

Such a decision in this project will be norms or not.

    2 answers 2

    As an option to do so:

     function ucfirst(str) { var at = str.charAt(0).toUpperCase(); return at + str.substr(1, str.length-1); } console.log(ucfirst('вася')); 

    • one
      str.length-1 why? - xes
    • @xes to tell the function the entire length at once, without counting inside the function. - And
     String.prototype.ucfirst=function() { return this.charAt(0).toUpperCase()+this.slice(1); } 

    console.log('вася'.ucfirst());

    • It will slowly work. - And
    • @And What exactly is slice or prototype ? - Ilya Indigo
    • @And In general, yes, instead of slice it is better to use substr , but if you go into detail, your str.length-1 crutch together with substr will still be slower, since the length of the strings and arrays in JS is dynamically calculated each time . - Ilya Indigo
    • one
      Where are the proofs billy? I need proofs. What are you generally talking about, what a dynamic count. The strings in js are not changeable and the length is stored immediately, without any recalculations, the length of the string cannot be changed. To the whole, str.length - 1 is the index of the last character. I agree that you can not write, but then substr will collect and consider itself to the end of the length , which is a fraction of a ms slower than making it clear what the entire length of the string is. - And
    • @And I apologize for the dynamic nature of the length property. I do not remember reading about it already, where in the for loop we did not recommend str.lenght as a condition in the comparison block and suggested storing it in the initialization block and comparing it from this variable. I didn’t find it, revising the MDN and the book of Flenigan, and even found the opposite. As for str.length - 1 this is the case when a programmer should not think of a compiler / interpreter, but simply follow the documentation for working with him. - Ilya Indigo