JavaScript Higher Order Functions Practice Exercises
map, filter, reduce, and composing behaviour
Challenges
forEach Visits Each Item
forEach Visits Each Item
`forEach` runs a callback once for every item in an array. It is useful when the...
Predict forEach Return
`forEach` runs the callback for each item, but it does not collect what the call...
forEach Side Effects
`forEach` is often used when each callback call should update outside state. The...
forEach Does Not Collect
`forEach` always returns `undefined`. It can run work for each item, but it will...
forEach for Side Effects
`forEach` is a good fit when the observable result is that work happened for eac...
forEach Does Not Break
A `return` inside a `forEach` callback exits only that callback run. It does not...
map Transforms Items
`map` creates a new array by transforming each item from the original array. The...
Map to Display Data
Mapping object arrays is a common step between stored data and UI text. Each sou...
Map Property Values
`map` is often the bridge from full records to the one field another part of the...
Map Callback Returns
A `map` callback must return the transformed value for each item. Arrow function...
Map to New Objects
`map` can transform an object array without editing the original objects. That m...
Map for View Models
Course card view models give JavaScript map a UI-shaped purpose. This hard exerc...
filter Keeps Matching Items
`filter` creates a new array containing only the items that pass a test. The cal...
Filter Records by State
Filtering object arrays is about preserving whole records whose properties pass ...
Filter Search Results
`filter` keeps original items that pass a test. In search code, that means the r...
Filter Without Removing
`filter` returns a new array and leaves the source array alone. That is differen...
Filter Without Truthiness Bugs
`filter` predicates often look like simple yes-or-no checks, but the exact condi...
Filter Business Rules
Coupon eligibility rules give JavaScript filter a business-shaped predicate. Thi...
find Returns One Item
`find` searches an array and returns the first item whose callback passes. It st...
Find and Missing Results
When `find` does not locate a match, it returns `undefined`. Production code oft...
Find the First Usable Record
`find` is the right shape when the caller needs one record, not every record tha...
Find Predicate Bugs
A `find` callback should test each item and return a boolean-like answer. If the...
Find With Multiple Conditions
`find` predicates can express more than one condition, but the result is still a...
Find by Priority
Use JavaScript find to resolve setting overrides across user, team, and global s...
some Answers Any
`some` returns a boolean. It answers whether at least one item passes the callba...
every Answers All
`every` returns `true` only when all items pass the callback test. A single fail...
some for Alerts
`some` answers whether at least one item passes a test. It is useful when the pr...
Combining some and every
`some` and `every` both return booleans, but they answer opposite collection que...
every and Empty Arrays
`every` returns `true` when no item fails the predicate. For an empty array, the...
Boolean Collection Audits
Practice JavaScript some and every by auditing admin accounts for inactive users...
reduce Combines Values
`reduce` turns an array into one accumulated result. The callback receives the c...
Reduce Object Data
`reduce` is useful when each item contributes part of one final value. With cart...
Reduce to One Choice
`reduce` can keep the best value seen so far, not only a running total. The accu...
Trace the Accumulator
`reduce` is easier to reason about when you track the accumulator after each cal...
Reduce Into an Object
`reduce` does not have to return a number. The accumulator can be an object that...
Reduce for Summaries
Group transaction amounts with JavaScript reduce while skipping voided records. ...
Reduce With a Summary Accumulator
Compute completed-attempt averages with JavaScript reduce while ignoring incompl...
flatMap Maps Then Flattens
`flatMap` runs a mapping callback, then flattens one level of arrays from the re...
One-to-Many Transforms
A single record often contains a nested list. Orders contain items, courses cont...
flatMap for Text Splitting
`flatMap` works well when each input item can produce several output values. A l...
Avoid Nested Map Results
`map` preserves one output slot for each input item. If each callback returns an...
flatMap With Empty Results
`flatMap` lets a callback return an empty array when a source item should contri...
flatMap for Nested Content
Visible lessons turn JavaScript flatMap into nested-content rendering practice. ...
Chain Compatible Results
Array methods chain when one method returns the kind of value the next method ex...
Chain Cleanup Steps
Chaining works best when each step has one job. A cleanup pipeline might trim va...
Chain Without Mutating
Chaining can express a data pipeline clearly: keep the records that matter, orde...
Chain Order Matters
Each method in a chain receives the result of the method before it. If an early ...
Chain Selection Before Ordering
A chain can express a small reporting pipeline: choose the records that matter, ...
Chained Data Pipelines
Build a JavaScript chaining pipeline that filters completed lessons, transforms ...
Chain a Search Pipeline
Design a JavaScript chaining search pipeline with query normalization, filtering...
Implement map
Writing a small version of `map` makes the callback contract visible. The helper...
Implement filter
A filter helper asks the callback whether each item should stay. The returned ar...
Implement forEach
Writing a small `forEach` helper makes its side-effect contract explicit. The he...
Implement reduce
Rebuild JavaScript reduce as a custom helper with an explicit initial value and ...
Implement find
Rebuild JavaScript find as a custom helper that returns the first original item ...
Implement flatMap
Practice higher-order JavaScript by implementing flatMap behavior without callin...