# Exercise: Game Score Functions

Write the four functions that keep score in a small video game, from points for coins to the end-of-level line, and let the tests check each one.

Apprentice · JavaScript · Updated October 30, 2026

Coin Dash is a small video game: Pip the fox runs across city rooftops, grabbing coins before the clock runs out. The game is drawn and the controls work, but nothing keeps score yet. The scoring rules are four small functions for you to write: points for coins, a bonus for finishing a level, a check for a new high score, and the line the game shows at the end. Tests check every one as you go.

## Get the exercise code

In the **Exercise** box beside the lesson, choose **Get the starter on GitHub** to open its GitHub repository. Click **Code**, then **Download ZIP**, and unzip it where you keep your practice folders.

The folder holds:

- `scores.js`: the four functions, for you to finish.
- `play.js`: plays one level of Coin Dash with your functions and prints the result. Ready to use.
- `scores.test.js`: the tests that check your functions. You don't need to change it.
- `package.json`: tells Node.js how to run the code and its tests. Ready to use.

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

## What to do

Every function is already declared, with its parameters, and returns a placeholder for now. The comment above each one says what it should return, with examples:

```javascript
// 1. pointsForCoins(coins)
//    Every coin is worth 10 points. Return the points for that many coins.
//    pointsForCoins(12) -> 120
//    pointsForCoins(0) -> 0
export function pointsForCoins(coins) {
  return 0;
}
```

The word `export` in front lets other files use the function: `play.js` and the tests bring it in from `scores.js`. You'll learn how that works in Module 3, in *Modules: import and export*. For now, leave `export` where it is.

1. **Finish `pointsForCoins`.** Every coin is worth 10 points.
2. **Finish `levelBonus`.** Finishing a level is worth 500 points, plus 10 for every second left on the clock, and a perfect run doubles the whole bonus. Give `perfectRun` a default value of `false`, so the game can call `levelBonus(30)` with one argument.
3. **Finish `isNewHighScore`.** It returns `true` only when the score beats the high score. Matching it isn't beating it.
4. **Finish `formatScore`, as an arrow function.** It returns a line such as `RocketRae scored 2210 points`, with `1 point` for a single point. Rewrite the function declaration as an arrow function stored in a `const`, and keep `export` in front.

Each function hands its answer back with `return`. None of them prints anything: `play.js` does the printing.

## Check your work

Run the tests from the terminal:

```bash
node --test
```

At first all 8 tests fail. That's expected: each function you finish turns its tests green. When a test fails, its message says what it expected, for example *12 coins are worth 120 points.*

To see your functions at work in a level of the game, run:

```bash
node play.js
```

## Hints

- **Work out an example by hand first.** On a perfect run with 30 seconds left, what's the bonus before it's doubled? And after?
- **Name the game's numbers.** A `const` at the top of the file, such as `const pointsPerCoin = 10;`, says what the 10 means (see *Variables with let and const*).
- **A comparison is already true or false.** `score > highScore` works out to a boolean, and a function can return it as it is.
- **A default goes in the parentheses.** *Function Expressions and Arrow Functions* shows how to give a parameter a value it uses when the caller leaves it out.
- **One word changes with the number.** The conditional operator, `? :`, from *switch and the Conditional Operator*, can pick `point` or `points` inside a template literal.

## When you're done

All 8 tests are green, and `node play.js` prints:

```text
*** LEVEL 3 COMPLETE! ***
Coins: 37 x 10 = 370
Level bonus: 1840
RocketRae scored 2210 points
NEW HIGH SCORE!
```

Now look back at your code. Could someone who has never played Coin Dash read `levelBonus` and know what each number in it means? If a bare 500 or 10 would make them guess, give it a name with `const`.

## So what

You've written the scoring rules of a real game as small, named functions, each with one job and a test to prove it works. That's how the score in every game you play is kept, and how every feature of every app is built: one small, tested function at a time.

## Key terms

- [Function Declaration](https://www.ask-codey.com/glossary/function-declaration)
- [Return Value](https://www.ask-codey.com/glossary/return-value-javascript-2)
- [Arrow Function](https://www.ask-codey.com/glossary/arrow-function)
- [Default Parameter](https://www.ask-codey.com/glossary/default-parameter)

## Related

- [What Is a Function](https://www.ask-codey.com/lessons/javascript-functions-what-is-a-function)
- [Parameters and Return Values](https://www.ask-codey.com/lessons/javascript-functions-parameters-and-return)
- [Function Expressions and Arrow Functions](https://www.ask-codey.com/lessons/javascript-functions-arrow-functions)

Source: https://www.ask-codey.com/lessons/exercise-javascript-game-score-functions
