mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-16 07:00:53 -04:00
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
1.9 KiB
1.9 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6821ebd4237de8297eaee791 | JavaScript Challenge 15: camelCase | 28 | javascript-challenge-15 |
--description--
Given a string, return its camel case version using the following rules:
- Words in the string argument are separated by one or more characters from the following set: space (
), dash (-), or underscore (_). Treat any sequence of these as a word break. - The first word should be all lowercase.
- Each subsequent word should start with an uppercase letter, with the rest of it lowercase.
- All spaces and separators should be removed.
--hints--
toCamelCase("hello world") should return "helloWorld".
assert.equal(toCamelCase("hello world"), "helloWorld");
toCamelCase("HELLO WORLD") should return "helloWorld".
assert.equal(toCamelCase("HELLO WORLD"), "helloWorld");
toCamelCase("secret agent-X") should return "secretAgentX".
assert.equal(toCamelCase("secret agent-X"), "secretAgentX");
toCamelCase("FREE cODE cAMP") should return "freeCodeCamp".
assert.equal(toCamelCase("FREE cODE cAMP"), "freeCodeCamp");
toCamelCase("ye old-_-sea faring_buccaneer_-_with a - peg__leg----and a_parrot_ _named- _squawk") should return "yeOldSeaFaringBuccaneerWithAPegLegAndAParrotNamedSquawk".
assert.equal(toCamelCase("ye old-_-sea faring_buccaneer_-_with a - peg__leg----and a_parrot_ _named- _squawk"), "yeOldSeaFaringBuccaneerWithAPegLegAndAParrotNamedSquawk");
--seed--
--seed-contents--
function toCamelCase(s) {
return s;
}
--solutions--
function toCamelCase(s) {
const words = s.replace(/[_\- ]+/g, ' ').split(' ');
return words.map((word, i) => {
if (i === 0) {
return word.toLowerCase();
} else {
const tempWord = word.split('');
return tempWord.shift().toUpperCase() + tempWord.join('').toLowerCase();
}
}).join('')
}