LoginSignup
0
0

More than 5 years have passed since last update.

js 単語の頭を大文字にして返す

Posted at

与えられた単語の頭を大文字にして返してください。
*与えられる文字はアルファベット

String.prototype.toJadenCase = function () {
  //...
};
var str = "How can mirrors be real if our eyes aren't real";
str.toJadenCase();

使ったもの

split();
map();
charAt();
toUpperCase();
slice();
concat()
join();

コード

String.prototype.toJadenCase = function () {
  let array = this.split(' ');
  let newArray = [];
  newArray = array.map((e,i)=>{
    let head = e.charAt(0).toUpperCase();
    let body = e.slice(1);
    return head.concat(body);
  });
  return newArray.join(' ');
};

その他コード

String.prototype.toJadenCase = function () { 
  return this.split(" ").map(function(e){
    return e.charAt(0).toUpperCase() + e.slice(1);
  }).join(" ");
}

他にもコードが浮かんだ方、
コードゴルフが浮かんだ方、コメントにお願いします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0