Convert to camelCase
A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.
The task
Convert `s` to camelCase. Words are separated by spaces, hyphens or underscores, possibly several in a row, and separators at the start or end are ignored. The first word is all lowercase; every later word gets an uppercase first letter and a lowercase rest, so `"API_KEY"` becomes `"apiKey"` and `"get HTTP response"` becomes `"getHttpResponse"`. An empty string returns `""`.
What this exercise teaches
Splitting on a run of separators and filtering empty strings handles repeated and leading separators in one step. Lowercasing each word before capitalizing is what turns API into Api; only capitalizing the first letter leaves KEY as KEY.
A hint if you get stuck
Split on `/[\s_-]+/`, drop empty words, lowercase every word, then uppercase the first letter of every word except the first and join.
Try it now, free, no account needed
Every exercise on Scriptdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. The worked solution opens once you solve it or after two tries.
Open this exerciseMore exercises in Strings and dates
- Make a URL slugFree
- Truncate with an ellipsisFree
- Format moneyFree
- Parse a query stringFree
- Align a text tableFree
- Add days to a dateFree
- Days between two datesFree
- Format a durationFree
- Last day of the monthFree
- Human readable file sizeFree
- Weekdays between two datesFree
- Run-length encodingFree
- Longest stretch without a repeatFree