Skip to content

Instantly share code, notes, and snippets.

@notsoluckycharm
Last active February 26, 2018 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notsoluckycharm/e9a59ca3729a7473571ed525c2ae227d to your computer and use it in GitHub Desktop.
Save notsoluckycharm/e9a59ca3729a7473571ed525c2ae227d to your computer and use it in GitHub Desktop.
CodeCademy Bowling
/**
# Given an array that represents **individual rolls** of a player in a game bolwing,
# write an algorithm to score the game.
#
# Example:
#
# Note: each score is a roll
# scores = [10,7,3,7,2,9,1,10,10,10,2,3,6,0,7,3,3]
# max = 22 rolls (10 frames + last spare -> bonus roll)
# spare score == 110
#
#
# There are 10 pins in a frame
# There are 10 frames in a match
#
#
# A strike is worth 10, plus the value of your next two rolls.
#
# A spare is worth 10, plus the value of your next roll.
#
# Open frames are scored as is (sum of rolls)
#
# Frame 1(20): 10 + (7 + 3); (score for the frame 10 +7 + 3)
# Frame 2(37): (3 + 7) + 7 ; (/, score for the frame 10 + 7)
#
*/
const calculateScore = (array, score = 0, newframe = true) => {
const [arr0, arr1] = array;
const spare = arr0 + arr1 === 10 && newframe;
const strike = arr0 === 10;
if(strike || spare){
return calculateScore(
array.slice(strike ? 1 : 2),
score + array.slice(0,3).reduce((x,y) => x + y, 0),
true
);
} else {
return arr1 !== undefined ? calculateScore(
array.slice(1),
score + arr0,
!newframe
) : score;
}
}
console.log(
calculateScore(
[10,7,3,7,2,9,1,10,10,10,2,3,6,0,7,3,3]
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment