JS Built-ins
← Array63 / 226
ArrayArray.prototype.filterbeginner

Array.prototype.filter() β€” filter by string length

πŸ“– Usage Example
const words = ['hi', 'hello', 'hey', 'howdy']
words.filter(w => w.length > 3)  // β†’ ['hello', 'howdy']

Use filter() to keep only strings that meet a length or pattern requirement.

Array.prototype.filter() β€” strings

filter() can be used on any array type. Filtering strings by their length is a common pattern for text processing.

Challenge: Implement filterLong(strs, minLen) that returns only strings with length >= minLen.

filterLong(['hi', 'hello', 'world', 'ok'], 4) // β†’ ['hello', 'world']
filterLong(['a', 'bb', 'ccc'], 2)             // β†’ ['bb', 'ccc']
πŸ’‘ Hints (2)
  • β€’ `s.length >= minLen` checks if the string meets the minimum length requirement.
  • β€’ `filter` preserves the order of elements.