-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·72 lines (67 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function getScoresMap () {
function isStrike (frame) {
return frame[0] === 10
}
function isSpare (frame) {
return frame[0] + frame[1] === 10
}
function getNextFrame (array, index) {
return array[index + 1]
}
function isLastFrame (index) {
return index === 9
}
function scoreFrame (frame) {
return frame.reduce((x, y) => x + y)
}
return (frame, index, array) => {
if (isLastFrame(index)) {
return scoreFrame(frame)
}
if (isStrike(frame)) {
const nextFrame = getNextFrame(array, index)
const nextnextFrame = getNextFrame(array, index + 1)
if (nextFrame === undefined) {
return undefined
} else if (isStrike(nextFrame)) {
if (nextnextFrame === undefined) {
return undefined
} else if (isStrike(nextnextFrame)) {
return 30
} else {
return 10 + 10 + scoreFrame(nextnextFrame)
}
} else {
return 10 + scoreFrame(nextFrame)
}
}
if (isSpare(frame)) {
const nextFrame = getNextFrame(array, index)
if (nextFrame === undefined) {
return undefined
} else {
return 10 + nextFrame[0]
}
}
return scoreFrame(frame)
}
}
function validateFrames (frames) {
if (frames.length > 10) {
throw Error('Frame length exceeds 10')
}
if (frames.length < 10) {
if (frames.some((f) => { return f.length > 2 })) {
throw Error('A maximum of two balls can scored for the first 9 frames')
} else if (frames.some((f) => { return f.length < 2 })) {
throw Error('Two balls must be thrown per frame')
}
} else if (frames[9][0] + frames[9][1] < 10 && frames[9].length > 2) {
throw Error('A maximum of two balls can be scored in the final frame total is less than 10')
}
}
function getFrameScores (frames) {
validateFrames(frames)
return frames.map(getScoresMap())
}
module.exports = getFrameScores