Aabha · Learn by building

Explained solution 05: Select event records

A: it reads the first event object's id. An unsuccessful find returns undefined; accessing undefined.title throws a TypeError. Test whether a record was found before using it.

B:

const available = events.filter((event) => {
  return event.places > 0;
});

Without return, the callback returns undefined, so no record qualifies. Parentheses-only arrow expressions return implicitly, but braces-based bodies need return.

C:

function creativeTitles(events) {
  return events
    .filter((event) => event.category === "creative" && event.places > 0)
    .map((event) => event.title);
}

The first operation selects records; the second transforms each selected record to a title. Empty input gives []. A creative event with zero places is excluded. This code does not write into any record, though filtered arrays still refer to the same objects.

Return to the exercise