レッスンに戻る

border-left-widthをborderLeftWidthに変換する

重要度: 5

「my-short-string」のような、ダッシュで単語が区切られた文字列をcamelCaseの「myShortString」に変換するcamelize(str)関数を作成します。

つまり、ダッシュをすべて削除し、ダッシュの後の単語はすべて大文字にします。

camelize("background-color") == 'backgroundColor';
camelize("list-style-image") == 'listStyleImage';
camelize("-webkit-transition") == 'WebkitTransition';

ヒント: 文字列を配列に分割して、変換して、joinで戻すためにsplitを使用します。

テスト付きのサンドボックスを開きます。

function camelize(str) {
  return str
    .split('-') // splits 'my-long-word' into array ['my', 'long', 'word']
    .map(
      // capitalizes first letters of all array items except the first one
      // converts ['my', 'long', 'word'] into ['my', 'Long', 'Word']
      (word, index) => index == 0 ? word : word[0].toUpperCase() + word.slice(1)
    )
    .join(''); // joins ['my', 'Long', 'Word'] into 'myLongWord'
}

テスト付きの解決策をサンドボックスで開きます。