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

bowling challenge #1615

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
31 changes: 31 additions & 0 deletions frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Frame {
// rest parameter syntax allows a function to accept an indefinite number of arguments as an array
// rest parameter(...args) will be an array
constructor (...scores){
this.frame = scores;
}

getFrame(){
return this.frame
}

sumFrame(){
// reduce() : return the sum of all the elements in an array
// reducer walks through the array element-by-element,
// at each step adding the current array value to the
// result from the previous step
return this.frame.reduce((sum, value) => sum + value, 0 )
}

isSpare(){
// The ! operator: check for the negation of a boolean value.
// (!this.isStrike())= (this.isStrike() != true)
return this.sumFrame() === 10 && !this.isStrike();
}

isStrike(){
return this.frame[0] === 10;
}
}

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


describe('Frame', () => {
describe('the first frame with two zero rolls', () => {
it('initialises with zero', () => {
const frame1 = new Frame(0,0);
expect(frame1.getFrame()).toEqual([0,0]);
});

it('sum of the initialised frame', () => {
const frame1 = new Frame(0,0);
expect(frame1.sumFrame()).toEqual(0);
})

it('frame is not a strike', () => {
const frame1 = new Frame(0, 0);
expect(frame1.isStrike()).toEqual(false);
})

it('frame is not a spare', () => {
const frame1 = new Frame(0, 0);
expect(frame1.isSpare()).toEqual(false);
});
});

describe('frame with two rolls upto 9', () => {
it('its array', () => {
const frame1 = new Frame(1,5);
expect(frame1.getFrame()).toEqual([1,5]);
});

it('sum of the frame', () => {
const frame1 = new Frame(1,5);
expect(frame1.sumFrame()).toEqual(6);
})

it('frame is not a strike', () => {
const frame1 = new Frame(1, 5);
expect(frame1.isStrike()).toEqual(false);
})

it('frame is not a spare', () => {
const frame1 = new Frame(1, 5);
expect(frame1.isSpare()).toEqual(false);
});
});

describe('when the frame is a spare', () => {
it('its array', () => {
const frame1 = new Frame(5,5);
expect(frame1.getFrame()).toEqual([5,5]);
});

it('sum of the frame', () => {
const frame1 = new Frame(5,5);
expect(frame1.sumFrame()).toEqual(10);
})

it('frame is not a strike', () => {
const frame1 = new Frame(5, 5);
expect(frame1.isStrike()).toEqual(false);
})

it('frame is a spare', () => {
const frame1 = new Frame(5, 5);
expect(frame1.isSpare()).toEqual(true);
});
});

describe('when the frame is a strike', () => {
it('its array', () => {
const frame1 = new Frame(10);
expect(frame1.getFrame()).toEqual([10]);
});

it('sum of the frame', () => {
const frame1 = new Frame(10);
expect(frame1.sumFrame()).toEqual(10);
})

it('frame is a strike', () => {
const frame1 = new Frame(10);
expect(frame1.isStrike()).toEqual(true);
})

it('frame is not a spare', () => {
const frame1 = new Frame(10);
expect(frame1.isSpare()).toEqual(false);
});
});

});
8 changes: 8 additions & 0 deletions frameindex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Frame = require('./frame');

const frame = new Frame(5,2)

console.log(frame.getFrame());
console.log(frame.sumFrame());
console.log(frame.isSpare());
console.log(frame.isStrike());
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Scorecard = require('./scorecard');

const scorecard = new Scorecard();
scorecard.addFrame(1, 2);
scorecard.addFrame(10, 0);
scorecard.addFrame(5, 5);
scorecard.addFrame(1, 2);
console.log(scorecard.getFrameArray());

console.log(scorecard.totalScores());
Loading