en.javascript.info/1-js/4-data-structures/1-string/1-ucfirst/solution.md
Ilya Kantor 87bf53d076 update
2014-11-16 01:40:20 +03:00

20 lines
No EOL
691 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Мы не можем просто заменить первый символ, т.к. строки в JavaScript неизменяемы.
Но можно пересоздать строку на основе существующей, с заглавным первым символом:
```js
//+ run
function ucFirst(str) {
var newStr = str.charAt(0).toUpperCase();
for(var i=1; i<str.length; i++) {
newStr += str.charAt(i);
}
return newStr;
}
alert( ucFirst("вася") );
```
P.S. Возможны и более короткие решения, использующие методы для работы со строками, которые мы пройдём далее.