Skip to content

Instantly share code, notes, and snippets.

@examinedliving
Created June 10, 2017 13:46
Show Gist options
  • Select an option

  • Save examinedliving/fd1a188344b0cba231a9c30a5db9784b to your computer and use it in GitHub Desktop.

Select an option

Save examinedliving/fd1a188344b0cba231a9c30a5db9784b to your computer and use it in GitHub Desktop.
Work in progress. Codewars kata for bowling pins.
//https://www.codewars.com/kata/5531abe4855bcc8d1f00004c/train/javascript
function scoreAt(frame, total, wasStrike, wasSpare) {
var strike = frame[0] === "X";
var spare = !strike && frame[1] === "/";
if (wasSpare) {
if (spare) {
return [total + (10 + frame[0]) + frame[0], false, true];
} else if (strike) {
return [total + (10 + 10 + 10), true, false];
} else {
return [total + 10 + frame[0] + frame[0], false, false];
}
}
if (wasStrike) {
if (spare) {
return [total + (10 + frame[0] + frame[1]) + (frame[0] + frame[1]), false, true];
} else if (strike) {
return [total + (10 + 10 + 10), true, false];
} else {
return [total + 10 + frame[0] + frame[0] + frame[1] + frame[1], false, false];
}
}
if (strike) {
return [total + 10, true, false];
}
if (spare) {
return [total + frame[0], false, true]
}
return [total + frame[0] + frame[1], false, false];
}
function bowlingScore(frames) {
var _total=0,_strike=false,_spare=false,retVal,arr,frame;
arr=frames.split(' ');
for(var i=0;i<arr.length;i++){
frame=arr[i].split('').map(function(e,index){return !isNaN(parseInt(e))?parseInt(e):e});
retVal=scoreAt(frame,_total,_strike,_spare);
_total=retVal[0];
_strike=retVal[1];
_spare=retVal[2];
}
return _total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment