# Exercise: High-Score Leaderboard

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.

Journeyman · JavaScript · Updated November 14, 2026

By Gregor Dzierzon

Comet Dash, the arcade game from this chapter, has a problem: its high-score list is one long array in the order the games were played, and the game's screens need it reshaped. The title screen wants the top 3. The stats screen wants a total and an average. The leaderboard wants numbered lines, best first. You'll write the six functions that do it, each with the array methods from this chapter, and tests check every one as you go.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-javascript-high-score-leaderboard](https://github.com/ask-codey/exercise-javascript-high-score-leaderboard), also linked from the **Exercise** box beside the lesson. Clone it into a folder of your own. In a terminal, in that folder:

```bash
git clone https://github.com/ask-codey/exercise-javascript-high-score-leaderboard.git
```

Git makes a folder, `exercise-javascript-high-score-leaderboard`, with the exercise code inside.

The folder holds:

- `leaderboard.js`: the six functions you'll finish.
- `high-scores.js`: Comet Dash's high-score list, eight games, ready to use.
- `show-leaderboard.js`: prints what your functions give back, so you can watch them work.
- `leaderboard.test.js`: the tests that check your functions. You don't need to change it.

Open the folder in VS Code, and open its terminal with **Terminal > New Terminal**.

## What to do

Every function in `leaderboard.js` already has its name and parameters, and returns `[]` or `0` for now. The comment above it says what it should return, with examples from the high-score list:

```javascript
/*
 * 2. Return the entries that scored more than `minimum` points, in the order they're in now.
 *    A score equal to `minimum` doesn't count as more.
 *
 *    playersOver(highScores, 9000) -> the entries for NovaFox, LunaByte and GlitchGoose
 *    playersOver(highScores, 20000) -> []
 */
export function playersOver(players, minimum) {
  return [];
}
```

1. **`topThree`**: the three best scores, highest first.
2. **`playersOver`**: the entries over a score.
3. **`displayLines`**: one line of text per entry, such as `PixelPanda: 8200 points (level 7)`.
4. **`totalScore`** and **`averageScore`**: the total of every score, and the average rounded to a whole number.
5. **`leaderboard`**: the entries at one level or higher, best first, as numbered lines, written as one chain.

Two rules hold for every function: use the chapter's array methods rather than `for` loops, and never change the list you're given. The game still needs it in the order the games were played.

## Check your work

Run the tests from the exercise folder:

```bash
node --test
```

At first every test fails. That's expected: each function you finish turns its test green. When a test fails, its message says what it expected, for example *Nobody scored over 20000, so playersOver should give an empty array.* If one says your function *changed the list it was given*, look for a `sort` that should be a `toSorted`.

To see your functions at work on the real list, run:

```bash
node show-leaderboard.js
```

## Hints

- **Name the job before you write the code.** "Keep these" is filter, "turn each one into a line" is map, "add them all up" is reduce, "put them in order" is sort. The table at the end of *Sorting Arrays* lines them up.
- **The top 3 is two steps.** Put the entries in order first, then take the first three with `slice(0, 3)`.
- **Give reduce a starting value.** The total of an empty list should be 0, not an error. *reduce* shows what happens without one.
- **Functions can call functions.** `averageScore` needs a total, and you'll already have a function that works one out.
- **Number the lines with the index.** map hands your callback each item's position as well as the item, as in *map and filter*. Positions start at 0, and leaderboards start at 1.

## When you're done

Every test is green, and `node show-leaderboard.js` prints:

```text
1) topThree: NovaFox, GlitchGoose, LunaByte
2) playersOver 9000: NovaFox, LunaByte, GlitchGoose
3) displayLines, the first line: PixelPanda: 8200 points (level 7)
4) totalScore: 56300
5) averageScore: 7038
6) leaderboard, level 5 and up:
   1. NovaFox: 12450
   2. GlitchGoose: 10300
   3. LunaByte: 9900
   4. PixelPanda: 8200
   5. WaffleWizard: 6750
   6. MossyMoose: 5150
```

Now look back at `leaderboard`. Read the chain aloud, one method per line. Does it say what the function does, in the order it does it? If someone asked for "the top 5 only", which one line would you add?

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. You don't need Git for this: you upload your files in the browser, as the lesson *Put Your Code on GitHub* (Module 1) shows screen by screen.

1. On GitHub, click **+**, then **New repository**. (No account yet? Sign up at github.com: it's free.) Name it (for example `exercise-javascript-high-score-leaderboard`), choose **Public**, turn **Add README** on so the upload button appears, and click **Create repository**. Your folder's own README replaces GitHub's when you upload it.
2. Choose **Add file**, then **Upload files**. In your exercise folder, select everything (Ctrl+A, or Cmd+A on a Mac) and drag it onto the upload area. Drag what's inside the folder, not the folder itself, so `package.json` lands at the top of your repo.
3. Under **Commit changes**, type a short note, such as *My solution*, and choose **Commit changes**.
4. Paste your repo's address, such as `https://github.com/your-name/exercise-javascript-high-score-leaderboard`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Upload the changed files again (each one replaces the file with the same name), commit, and submit again: your latest submission is the one that counts.

## So what

Taking a raw list and reshaping it for a screen, by keeping some items, ordering them and turning them into lines, is a job you'll do in nearly every app you build, from a game's leaderboard to a shop's search results. You now have the four tools that do it.

## Key terms

- [map()](https://www.ask-codey.com/glossary/map-javascript)
- [filter()](https://www.ask-codey.com/glossary/filter-javascript)
- [reduce()](https://www.ask-codey.com/glossary/reduce-javascript)
- [sort()](https://www.ask-codey.com/glossary/sort-javascript)
- [Callback Function](https://www.ask-codey.com/glossary/callback-function)

## Related

- [Functions as Values: Callbacks](https://www.ask-codey.com/lessons/javascript-callbacks-functions-as-values)
- [map and filter](https://www.ask-codey.com/lessons/javascript-array-methods-map-and-filter)
- [reduce](https://www.ask-codey.com/lessons/javascript-array-methods-reduce)
- [Sorting Arrays](https://www.ask-codey.com/lessons/javascript-array-methods-sort)

Source: https://www.ask-codey.com/lessons/exercise-javascript-high-score-leaderboard
