Skip to content

Instantly share code, notes, and snippets.

@kevbuchanan
Created September 18, 2013 03:58
Show Gist options
  • Save kevbuchanan/6604376 to your computer and use it in GitHub Desktop.
Save kevbuchanan/6604376 to your computer and use it in GitHub Desktop.
This ruby quiz, http://rubyquiz.strd6.com/quizzes/181-bowling-scores, using js and jasmine.
function Frame(rolls) {
if (rolls[0] == 10) {
this.rolls = [rolls[0]]
}
else {
this.rolls = rolls
}
}
Frame.prototype.rollScore = function() {
return this.rolls.reduce(function(previosValue, currentValue){
return previosValue + currentValue
})
}
Frame.prototype.strike = function() {
return this.rolls[0] == 10
}
Frame.prototype.spare = function() {
return this.rollScore() == 10 && !this.strike()
}
Frame.prototype.bonusRolls = function() {
if (this.strike()) {
return 2
}
else if (this.spare()) {
return 1
}
else {
return 0
}
}
Frame.prototype.firstRoll = function() {
if (this.strike()) {
return "X"
}
else if (this.rolls[0] == 0) {
return "-"
}
else {
return String(this.rolls[0])
}
}
Frame.prototype.lastRoll = function() {
if (this.strike()) {
return ""
}
else if (this.rolls[1] == 0) {
return "-"
}
else if (this.spare()) {
return "/"
}
else {
return String(this.rolls[1])
}
}
function Scorecard(input) {
var splitInput = input.split(" ")
this.bowler = splitInput[0]
splitInput.shift()
this.rolls = splitInput.map(function(element) {
return parseInt(element)
})
this.frames = this.generateFrames()
}
Scorecard.prototype.generateFrames = function() {
var frames = []
var newRolls = this.rolls.join(" ").replace(/10/g, "10 0").split(" ").map(function(element) {
return parseInt(element)
})
newRolls.eachSlice(2, function(slice) {
frames.push(new Frame(slice))
})
return frames
}
Scorecard.prototype.scoreFor = function(index) {
var frame = this.frames[index]
nextFrameRolls = []
if (!!this.frames[index + 1]) {
nextFrameRolls = (this.frames[index + 1].rolls)
}
if (!!this.frames[index + 2]) {
nextFrameRolls = nextFrameRolls.concat(this.frames[index + 2].rolls)
}
var extraScore = nextFrameRolls.splice(0, frame.bonusRolls()).reduce(function(previosValue, currentValue) {
return previosValue + currentValue
}) || 0
return frame.rollScore() + extraScore
}
Scorecard.prototype.totalScore = function() {
var score = 0
for (var i = 0; i < 10; i++) {
score += this.scoreFor(i)
}
return score
}
Array.prototype.eachSlice = function(size, callback) {
for (var i = 0, l = this.length; i < l; i += size) {
callback.call(this, this.slice(i, i + size))
}
}
describe("Frame", function() {
var frame
var strikeFrame
var spareFrame
beforeEach(function() {
frame = new Frame([6, 2])
strikeFrame = new Frame([10, 0])
spareFrame = new Frame([8, 2])
})
it("should have a rolls array", function() {
expect(strikeFrame.rolls).toEqual([10])
})
describe("#rollScore", function() {
it("should return the sum of the rolls array", function() {
expect(frame.rollScore()).toEqual(8)
})
})
describe("#strike", function() {
it("should return true if the frame was a strike", function() {
expect(strikeFrame.strike()).toEqual(true)
})
it("should return false if the frame was not a strike", function() {
expect(spareFrame.strike()).toEqual(false)
})
})
describe("#spare", function() {
it("should return true if the frame was a spare", function() {
expect(spareFrame.spare()).toEqual(true)
})
it("should return false if the frame was not a spare", function() {
expect(strikeFrame.spare()).toEqual(false)
})
})
describe("#bonusRolls", function() {
it("should return 2 for a strike", function() {
expect(strikeFrame.bonusRolls()).toEqual(2)
})
it("should return 1 for a spare", function() {
expect(spareFrame.bonusRolls()).toEqual(1)
})
it("should return 0 for any other roll", function() {
expect(frame.bonusRolls()).toEqual(0)
})
})
describe("#firstRoll", function() {
it("should return a X for a strike", function() {
expect(strikeFrame.firstRoll()).toEqual("X")
})
it("should return a - for a 0 roll", function() {
var zeroFrame = new Frame([0, 5])
expect(zeroFrame.firstRoll()).toEqual("-")
})
it("should return a number for any other roll", function() {
expect(frame.firstRoll()).toEqual("6")
})
})
describe("#lastRoll", function() {
it("should return an empty string for a strike", function() {
expect(strikeFrame.lastRoll()).toEqual("")
})
it("should return a - for a 0 roll", function() {
var zeroFrame = new Frame([5, 0])
expect(zeroFrame.lastRoll()).toEqual("-")
})
it("should return a / for a spare", function() {
expect(spareFrame.lastRoll()).toEqual("/")
})
it("should return a number for any other roll", function() {
expect(frame.lastRoll()).toEqual("2")
})
})
})
describe("Scorecard", function() {
var scorecard
beforeEach(function() {
scorecard = new Scorecard("Kevin 10 10 10 10 10 10 10 10 10 10 10 10")
})
it("should have a bowler name", function() {
expect(scorecard.bowler).toEqual("Kevin")
})
it("should have a rolls array", function() {
expect(scorecard.rolls).toEqual([10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10])
})
describe("#generateFrames", function() {
it("should return an array of frames", function() {
expect(scorecard.generateFrames()[0]).toEqual(jasmine.any(Frame))
})
})
describe("#scoreFor", function() {
it("should return the correct score for a given frame index", function() {
expect(scorecard.scoreFor(1)).toEqual(30)
})
})
describe("#totalScore", function() {
it("should return the correct score for the round", function() {
expect(scorecard.totalScore()).toEqual(300)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment