Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull request #1618

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bowling/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
33 changes: 33 additions & 0 deletions bowling/bowling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Scorecard {
constructor() {
this.scorecard = [];
}

addFrame(...frames) {
if (this.scorecard.length < 10) {
this.scorecard.push([...frames]);
} else {
return 'This game has been completed already.';
}
}

calculateScore() {
let total = 0;
let counter = 1;
this.scorecard.forEach((frame) => {
let roll = frame.reduce((a, b) => {
return a + b;
});
if (roll >= 10 && counter < 10 && counter != this.scorecard.length) {
total += roll ;
total += this.scorecard[counter][0];
} else {
total += roll;
}
counter += 1
});
return total;
}
}

module.exports = Scorecard;
63 changes: 63 additions & 0 deletions bowling/bowling.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { test } = require('node:test');
const Scorecard = require('./bowling');

describe('scorecard methods', () => {
it('returns a score of 0 at start of game ', () => {
player1 = new Scorecard;
expect(player1.calculateScore()).toBe(0);
});
it('returns a score of 9 after 1 frame', () => {
player1 = new Scorecard;
player1.addFrame(5, 4)
expect(player1.calculateScore()).toBe(9);
});
it('adds the first roll of the next frame if current frame is 10+', () => {
player1 = new Scorecard;
player1.addFrame(5, 5)
player1.addFrame(4, 5)
expect(player1.calculateScore()).toBe(23);
});
it('Does not allow user to add 10+ frames ', () => {
player1 = new Scorecard;
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10, 10)
expect(player1.addFrame(10, 10, 10)).toBe('This game has been completed already.');
});
it('is able to count a Perfect Game ', () => {
player1 = new Scorecard;
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10)
player1.addFrame(10, 10, 10)
expect(player1.calculateScore()).toBe(300);
});
it('is able to count a Gutter Game ', () => {
player1 = new Scorecard;
player1.addFrame(0, 0)
player1.addFrame(0, 0)
player1.addFrame(0, 0)
player1.addFrame(0, 0)
player1.addFrame(0, 0)
player1.addFrame(0, 0)
player1.addFrame(0, 0)
player1.addFrame(0, 0)
player1.addFrame(0, 0)
player1.addFrame(0, 0)
expect(player1.calculateScore()).toBe(0);
});

});
Loading