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
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:
// 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.
- Finish
pointsForCoins. Every coin is worth 10 points. - 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. GiveperfectRuna default value offalse, so the game can calllevelBonus(30)with one argument. - Finish
isNewHighScore. It returnstrueonly when the score beats the high score. Matching it isn't beating it. - Finish
formatScore, as an arrow function. It returns a line such asRocketRae scored 2210 points, with1 pointfor a single point. Rewrite the function declaration as an arrow function stored in aconst, and keepexportin 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:
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:
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
constat the top of the file, such asconst pointsPerCoin = 10;, says what the 10 means (see Variables with let and const). - A comparison is already true or false.
score > highScoreworks 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 pickpointorpointsinside a template literal.
When you're done
All 8 tests are green, and node play.js prints:
*** 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.