In this challenge we're going to score a bowling game, given a set of frames. Easier said than done.
Depending on where you're from (or your interests) you might either be very familiar or not at all certain about the rules of bowling.
The rules of ten-pin bowling
Here we're not speaking of cricket, or lawn bowls! Ten pins in a lane, rental shoes that smell of disinfectant, coloured balls with holes that never seem to quite fit your fingers... that's the kind of bowling we want to score.
- Each player usually gets two balls per frame. There are ten frames.
- If a player knocks down all the pins with one ball, that's a strike.
- A strike scores ten points plus the score for the next two balls.
- If a player knocks down all the pins with two balls, that's a spare.
- A spare scores ten points plus the score for the next ball.
- If a player doesn't knock down all the pins in a frame, they just get one point for each pin they do manage to knock down.
- On the tenth frame, if the first two balls contain a strike or make a spare, the player gets a third ball.
- The maximum possible score (a "perfect game" of all strikes) is 300.
This WikiHow page has some more detail on how to keep score.
We can write our code in game.js
-
Using what we know about JavaScript, write a program which scores a full game, given all frames
More about scoring a game
Assume we already know how many pins were knocked down in each frame. You can come up with your own way to represent that data, but we suggest you use something like this example:
const frames = [ [1, 2], [6, 4], [5, 4], [10, 0], [7, 2], [10, 0], [10, 0], [5, 2], [7, 0], [4, 4] ]
The score for this game is 119.
-
Try scoring a game with a more complex ending
A game with a complex ending
The frames of a game with a more complex ending might look like:
const frames = [ [1, 2], [6, 4], [5, 4], [10, 0], [7, 2], [10, 0], [10, 0], [5, 2], [7, 0], [10, 10, 10] ]
The score for this game is 141.
-
Try scoring a perfect game
A perfect game
The frames of a perfect game would look like:
const frames = [ [10, 0], [10, 0], [10, 0], [10, 0], [10, 0], [10, 0], [10, 0], [10, 0], [10, 0], [10, 10, 10] ]
The score for this game is 300.
If you would benefit from an interactive illustration, check out this Bowling Score Calculator.
If we've written our code in game.js
, we can execute it by running:
node game.js
Remember, we will only see output from running our game.js
file if our code has console.log
calls.