Makers Academy bowling scorecard solution with Jasmine
I've tried to give an example of using tests to fully specify the solution.
Run SpecRunner.html and read the output to see the extent of the solution. I don't warrant that every eventuality is covered, but the obvious paths are.
The solution uses a linked-list-style approach, with each frame having a reference to the next frame, which it uses to calculate its own score (in the case of a strike or spare).
The game can then walk this list (starting at the first frame) to calculate the total score.
Note how bowl()
methods return this
to allow chaining. This is provided merely as a convenience, but makes for a nice interface.
FinalFrame
uses a 'classical' inheritance pattern - i.e. it's aim is to construct objects whose prototype inherits the prototype of Frame
and whose constructor also calls the Frame
constructor.
This is achieved by overwriting the FinalFrame
function's prototype with a new object derived from the Frame
prototype:
FinalFrame.prototype = Object.create(Frame.prototype);
However, this means FinalFrame
's prototype (the new object just created above) now inherits its constructor
property from the prototype of Frame
; which returns Frame
, which we don't want, so we overwrite the property:
FinalFrame.prototype.constructor = FinalFrame;
Finally, we call Frame
from FinalFrame
to ensure new objects are correctly initialized:
function FinalFrame() {
Frame.call(this);
};