This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const bowlingScoreCard = (frames) => { | |
let scoreCard = ''; | |
frames.forEach((frameRolls) => { | |
const frameResult = calculateFrame(frameRolls[0], frameRolls[1]); | |
scoreCard += frameResult; | |
}); | |
return scoreCard; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('totalScore function', () => { | |
test('it should return total score when no spares/strike', () => { | |
const scoreCard = '2222222222' | |
const totalScore = totalScore(scoreCard); | |
expect(totalScore).toBe(20); | |
}); | |
test('it should return total score when we have spares', () => { | |
const scoreCard = '/2222222/2' | |
const totalScore = totalScore(scoreCard); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const totalScore = (scoreCard, frames) => { | |
let score = 0; | |
scoreCard.forEach((roundScore, index) => { | |
if(roundScore === '/') { | |
score += 10 + frames[index + 1][0]; | |
} else { | |
score += parseInt(roundScore) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (roundScore === 'X') { | |
score += 10 + frames[index + 1][0] + frames[index + 1][1]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('it should return total score when we have consecutive strikes', () => { | |
const scoreCard = '/2XX2222/2' | |
const totalScore = totalScore(scoreCard); | |
expect(totalScore).toBe(67); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const listToMatrix = (list) => { | |
let matrix = [], i, k; | |
for (i = 0, k = -1; i < list.length; i++) { | |
if (i % 2 === 0) { | |
k++; | |
matrix[k] = []; | |
} | |
matrix[k].push(list[i]); | |
} | |
return matrix; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const bowlingScore = (inputRolls) => { | |
if (inputRolls.length < 20 || inputRolls.length > 21 || inputRolls.some(roll => roll < 0)) { | |
return 'Invalid rolls input' | |
} | |
const arrayOfRolls = listToMatrix(inputRolls); | |
return totalScore(arrayOfRolls); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('bowlingScoreCard function', () => { | |
test('it should correct scorecard when no strikes/spares', () => { | |
const frames = [ | |
[1, 1], | |
[1, 1], | |
[1, 1], | |
[1, 1], | |
[1, 1], | |
[1, 1], | |
[1, 1], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (roundScore === 'X') { | |
// handle Strike | |
const futurePoint = frames[index + 2] ? frames[index + 2][0] : 10; | |
const nextPoint = frames[index + 1][1] || frames[index + 1][0] !== 10 | |
? frames[index + 1][1] | |
: futurePoint; | |
if (nextPoint) { | |
roundScore = 10 + frames[index + 1][0] + nextPoint; | |
} else { | |
roundScore = 10 + frames[index + 1][0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const calculateFrame = (rollOne, rollTwo) => { | |
const score = rollOne + rollTwo; | |
if (score > 10) { | |
return 'Invalid frame input'; | |
} | |
if (rollOne === 10) { | |
return 'X'; | |
} | |