Capitalize each word
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
Words are separated by single spaces. Uppercase the first character of every word and lowercase the rest of it. Keep the spacing exactly as it is, including repeated spaces and spaces at the ends.
What this exercise teaches
Splitting on a single space keeps empty strings where spaces repeat, and joining with a single space restores the original spacing. Using the first character and slice(1) works for accented letters too, unlike a regular expression with \b, which only knows ASCII letters.
A hint if you get stuck
Split on `" "` (not on `/\s+/`), so empty strings between repeated spaces survive and the join puts the spaces back. Skip empty words.
This exercise is part of Pro
Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.
You create a free account first, so Pro is attached to it.
Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.
More exercises in Basics
- Total of a listFree
- Items in stockFree
- Full namesFree
- Has every tagFree
- Reverse the wordsFree
- Count the vowelsPro
- Clamp a numberPro
- Range of numbersPro