retour au cours

Traduit border-left-width en borderLeftWidth

importance: 5

Ecrivez la fonction camelize(str) qui change les mots séparés par des tirets comme “my-short-string” en camel-cased “myShortString”.

La fonction doit donc supprimer tous les tirets et mettre en majuscule la première lettre de chaque mot à partir du deuxième mot.

Exemples :

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

P.S. Astuce : utilisez split pour scinder la chaîne dans un tableau, transformer la et ensuite utilisez join.

Open a sandbox with tests.

function camelize(str) { return str .split('-') // divise 'my-long-word' en tableau ['my', 'long', 'word'] .map( // capitalise les premières lettres de tous les éléments du tableau sauf le premier // convertit ['my', 'long', 'word'] en ['my', 'Long', 'Word'] (word, index) => index == 0 ? word : word[0].toUpperCase() + word.slice(1) ) .join(''); // rejoint ['my', 'Long', 'Word'] en -> myLongWord }

Ouvrez la solution avec des tests dans une sandbox.