Created
September 8, 2021 04:39
-
-
Save Aldizh/0edf25d0d537ca2f2089784793ba8062 to your computer and use it in GitHub Desktop.
Bowling game calculator
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
// Installed npm packages: jquery underscore request express | |
// jade shelljs passport http sys lodash async mocha chai sinon | |
// sinon-chai moment connect validator restify ejs ws co when | |
// helmet wrench brain mustache should backbone forever debug jsdom | |
/* | |
Please write a function to score a game of bowling, | |
special scoring considerations | |
====================================== | |
X (strike) => bonus (score from next two throws) | |
/ (spare) => bonus (score from next throw) | |
An example game | |
|-----|-----|-----|-----|-----|-----|-----|-----|-----|-------| | |
| 1 7 | 2 / | X | X | X | 6 / | 7 2 | 1 3 | 4 5 | 1 / 7 | | |
| 8 | 28 | 58 | 84 | 104 | 121 | 130 | 134 | 143 | 160 | | |
|-----|-----|-----|-----|-----|-----|-----|-----|-----|-------| | |
*/ | |
function helper(char, previous) { | |
if (char === '/') return 10 - parseInt(previous) | |
if (char === 'X') return 10 | |
return parseInt(char) | |
} | |
function score(input) { | |
const strikeScore = 10; | |
const frames = 10 | |
let final = 0 | |
// return final score | |
const scoreArray = input.split('') | |
for (var i=0; i<frames; i = i+2) { | |
if (parseInt(scoreArray[i]) + parseInt(scoreArray[i+1]) < 10) { | |
// spares and strikes will be skipped here | |
final += parseInt(scoreArray[i]) + parseInt(scoreArray[i+1]) | |
} | |
// if scores is a strike advance only by one | |
if (scoreArray[i] === 'X') { | |
i--; | |
} | |
// can only have three max | |
final += ( | |
helper(scoreArray[i], 0) + | |
helper(scoreArray[i+1], scoreArray[i]) + | |
helper(scoreArray[i+2], scoreArray[i+1]) | |
) | |
} | |
return final | |
} | |
const input = '172/XXX6/7213451/7' | |
// score(input) => 160 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment