Transforming Data

Hand a function to another function and let it do the looping. With map, filter, reduce and sort you can turn a raw list into a leaderboard in a few readable lines.

Updated

Functions as Values

  1. Functions as Values: Callbacks (Apprentice): In JavaScript a function is a value you can hand to another function, which calls it for you; that handed-over function is a callback, and it's how sorting, timers and buttons work.

Array Methods in JavaScript

  1. map and filter (Apprentice): map turns every item of an array into something new, filter keeps only the items that pass a test, and chained together they turn raw data into exactly the list a screen needs.
  2. reduce (Journeyman): reduce walks through an array carrying a running total, so a whole list of scores, prices or plays boils down to one answer, such as a total, a count or a best score.
  3. Sorting Arrays (Apprentice): sort puts an array in order, but it compares items as text unless you give it a compare function, and it changes the array itself, so toSorted is the way to get a sorted copy and keep the original.

Exercise: High-Score Leaderboard

  1. Exercise: High-Score Leaderboard (Journeyman): Finish six functions that turn an arcade game's high-score list into its top 3, display lines, totals and a numbered leaderboard, using map, filter, reduce and sort, and let the tests check each one.