[JS Practice] Name Array Capping (第一个字母大写)

Title :

Name Array Capping (第一个字母大写)

Description:

Create a method that accepts an array of names, and returns an array of each name with its first letter capitalized.

完成一个方法,实现当方法在接收到一个以字符串为元素的数组时,方法能否使每个元素的第一个字符大写。

example :

capMe([<span class="string">'jo'</span>, <span class="string">'nelson'</span>, <span class="string">'jurie'</span>]) <span class="comment">// returns ['Jo', 'Nelson', 'Jurie']</span> 
capMe([<span class="string">'KARLY'</span>, <span class="string">'DANIEL'</span>, <span class="string">'KELSEY'</span>]) <span class="comment">// returns ['Karly', 'Daniel', 'Kelsey']</span>

My solution :

function capMe(names) {
  var retArr = [];
  names.forEach(function(name){
    retArr.push(capFirstLetter(name));
  });
  return retArr;
}

/*
 * This function translate 
 * @param : string
 * @return : stirng with first letter capitalized
 */
function capFirstLetter(str) {
  return str.charAt(0).toUpperCase() + str.toLowerCase().slice(1);
}

Ref. from codewars

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s