Night and Day explainer


The aim of this puzzle: Filter the departures to get the evening times and store it in pmTimes

Walkthrough of the solution: The departures is an array of strings. Each string is a number with followed by either 'am' or 'pm'. The morning and evening functions can be used as callbacks in the .filter() method. The .filter() uses a callback function as a check. If the check is true then it passes the filter. If the check is false then it doesn’t pass the filter. When .filter(morning) is applied to the departures array, only the elements that include 'am' will pass the filter.

To filter the departures array and get the 'pm' times, you can use .filter(evening) on the array. It’s important to know that the .filter() doesn’t change the contents of the array; it just returns a modified copy, in this case, a copy with only the elements with 'pm'.

Sample code solution:
(Tap below to reveal)

import { departures } from '';

function morning(time) {
  return time.includes('am');

function evening(time) {
  return time.includes('pm');

let amTimes = departures.filter(morning);
let pmTimes = departures.filter(evening);

console.log('Day times: ' + amTimes);
console.log('Night times: ' + pmTimes);

JavaScript Concepts: Callback Functions, Calling Functions, Code Block (function), console.log(), .filter(), import