Break the problem into steps
A flat-pack shelf arrives as one picture and a bag of screws. You do not build it in a single motion; you follow the instructions, one small step at a time, and each step is easy to check. Writing code works the same way.
Take this task: turn a title into a URL slug.
Input:
" Hello, World! "Output:"hello-world"
Read the two ends and the gap looks wide. Do not write the whole transform in one go. Name the small steps that close it, in order.
First, the string has spaces at the edges — trim them. Second, slugs are lowercase — lower the case. Third, the punctuation and the run of spaces in the middle should become single hyphens — replace every run of characters that are not letters or digits with one hyphen. Fourth, that can leave a hyphen at either end — trim the hyphens.
Four steps. None is hard on its own, and each has a name you could say out loud. Put them in a row and the messy title walks out the far side clean.
Notice what the split bought you. You never held the whole transform in your head at once. You checked each step against the example: the trim tidies the edges, the lower makes it uniform, the replace collapses the middle, and the last trim removes any hyphen left dangling.
Export slugify from index.js. Build it as those small steps, in order, each
one handing its result to the next.
Challenge
A title arrives with stray spaces, mixed case, and punctuation, and it has to become a URL slug you could drop into a link. Doing the whole transform in your head is easy to get wrong at the edges.
Export a slugify function from index.js. It takes a title string and
returns the slug: lowercased, with every run of characters that are not letters
or digits replaced by a single hyphen, and no hyphen left at either end.