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

unfinished- able to add bonus points for strike and spare #1605

Open
wants to merge 2 commits 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
41 changes: 41 additions & 0 deletions frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Frame {
constructor(roll1, roll2) {
if (roll1 > 10 || roll1 + roll2 > 10) {
throw new Error("invalid points");
}

this.roll1 = roll1;
this.roll2 = roll2;
}

getRoll1() {
return this.roll1;
}

basePoints() {
return this.roll1 + this.roll2;
}

bonusPoints(nextFrame) {
let points = 0;

if (this.roll1 === 10) {
points += nextFrame.basePoints();
} else if (this.roll1 + this.roll2 === 10) {
points += nextFrame.getRoll1();
}

return points;
}

}

// try {
// const frame1 = new Frame(8, 2); // This will work
// const frame2 = new Frame(10, 1); // This will throw an error
// } catch (error) {
// console.error(error.message); // Output: "Invalid points"
// }


module.exports = Frame;
39 changes: 39 additions & 0 deletions integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const Scorecard = require('./scorecard');
const Frame = require('./frame');

describe('integration', () => {
let scorecard;
beforeEach(() => { scorecard = new Scorecard() })

it('returns zero points when player never hits a pin in all 10 frames', () => {
for (let i = 0; i < 10; i++) {
frame = new Frame(0, 0);
scorecard.addFrame(frame);
}
expect(scorecard.calculateScore()).toEqual(0);
})

it('returns 22 when 1st frame is a spare and next frame has 5 and 2 points', () => {
frame1 = new Frame(6, 4);
frame2 = new Frame(5, 2);

scorecard.addFrame(frame1);
scorecard.addFrame(frame2);

expect(scorecard.calculateScore()).toEqual(22);
})

it('returns 28 when 1st frame is a strike and next frame has 5 and 2 points', () => {
frame1 = new Frame(10, 0);
frame2 = new Frame(5, 4);

scorecard.addFrame(frame1);
scorecard.addFrame(frame2);

expect(scorecard.calculateScore()).toEqual(28);
})


})


Loading