Scriptdrill

Level 3: Strings and dates Exercise 43 of 76 Free

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 exercise

More exercises in Strings and dates