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

Progress Update, 2/5/23 #1603

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
21 changes: 21 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
env: {
browser: true,
es2021: true
},
extends: [
'plugin:react/recommended',
'standard'
],
overrides: [
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
plugins: [
'react'
],
rules: {
}
}
64 changes: 64 additions & 0 deletions frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class Frame {
constructor (round, frame) {
// Initializes the properties of the Frame instance
this.round = round; // The round number
this.frame = frame; // An array representing the frame (e.g., [3, 5])
this.isStrike = this.getStrike(); // A boolean indicating if the frame is a strike
this.isSpare = this.getSpare(); // A boolean indicating if the frame is a spare
this.isValid = this.checkValid(); // A boolean indicating if the frame is valid
}

getIndex () {
// Returns the zero-based index of the frame
return this.round - 1;
}

getFrame () {
// Returns the array representing the frame
return this.frame;
}

checkValid () {
// Checks if the sum of the pins in the frame is less than or equal to 10
if (this.getTotal() < 11) {
return true; // If the frame is valid, returns true
} else {
return false; // If the frame is invalid, returns false
}
}

getValid () {
// Returns a boolean indicating if the frame is valid
return this.isValid;
}

getTotal () {
// Calculates and returns the total number of pins in the frame
let frameTotal = 0;
this.frame.forEach(obj => {
frameTotal += obj;
})
return frameTotal;
}

getStrike () {
// Checks if the first roll in the frame knocked down all 10 pins
if (this.frame[0] === 10) {
return true; // If the frame is a strike, returns true
} else {
return false; // If the frame is not a strike, returns false
}
}

getSpare () {
// Checks if the frame knocked down all 10 pins in two rolls
if (this.getTotal() === 10 && this.getStrike() === false) {
return true; // If the frame is a spare, returns true
} else {
return false; // If the frame is not a spare, returns false
}
}
}

module.exports = Frame;

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

describe('Creates frame object:', () => {
it('Creates empty frame', () => {
const result = new Frame(0, [])
expect(result.getFrame()).toEqual([])
})

it('Creates frame with score', () => {
const result = new Frame(0, [1, 2])
expect(result.getFrame()).toEqual([1, 2])
})

it('Returns total score for frame', () => {
const result = new Frame(0, [1, 2])
expect(result.getTotal()).toBe(3)
})

it('Returns strike when strike is entered', () => {
const result = new Frame(0, [10, 0])
expect(result.getStrike()).toBe(true)
})

it('Returns not strike when [2, 4] is entered', () => {
const result = new Frame(0, [2, 4])
expect(result.getStrike()).toBe(false)
})

it('Returns spare when spare [5, 5] is entered', () => {
const result = new Frame(0, [5, 5])
expect(result.getSpare()).toBe(true)
})

it('Returns spare when spare [1, 9] is entered', () => {
const result = new Frame(0, [1, 9])
expect(result.getSpare()).toBe(true)
})

it('Returns not spare when [1, 7] is entered', () => {
const result = new Frame(0, [1, 7])
expect(result.getSpare()).toBe(false)
})

it('Returns frame validity on [1, 2] as true', () => {
const result = new Frame(0, [1, 2])
expect(result.getValid()).toBe(true)
})

it('Returns frame validity on [7, 7] as false', () => {
const result = new Frame(0, [7, 7])
expect(result.getValid()).toBe(false)
})
})
Loading