Higher Order Functions

flatMap for Text Splitting

flatMap for Text Splitting

flatMap works well when each input item can produce several output values. A line of text can become multiple words, while a blank line should contribute nothing.

Using map alone would leave you with an array of word arrays. The caller wants one list it can count, search, or render without another flattening step.

Your task: Complete wordsFromLines so it returns one flat array of lowercase words from the provided lines. Trim each line, skip blank lines, and preserve the order the words appear in.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

flatMap for Text Splitting

flatMap works well when each input item can produce several output values. A line of text can become multiple words, while a blank line should contribute nothing.

Using map alone would leave you with an array of word arrays. The caller wants one list it can count, search, or render without another flattening step.

Your task: Complete wordsFromLines so it returns one flat array of lowercase words from the provided lines. Trim each line, skip blank lines, and preserve the order the words appear in.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Console

Run code to see output.