# 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 November 14, 2026

By Gregor Dzierzon

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

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-javascript-game-score-functions](https://github.com/ask-codey/exercise-javascript-game-score-functions), also linked from the **Exercise** box beside the lesson. Clone it where you keep your practice folders. In a terminal, in that folder:

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

Git makes a folder, `exercise-javascript-game-score-functions`, with the exercise code inside.

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`.

## 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-game-score-functions`), 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-game-score-functions`, 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

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 (JavaScript)](https://www.ask-codey.com/glossary/return-value-javascript)
- [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
