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
Open this lesson's GitHub repository, 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:
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:
// 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.
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.
- 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. - 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.jsonlands at the top of your repo. - Under Commit changes, type a short note, such as My solution, and choose Commit changes.
- 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.