-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (37 loc) · 1.28 KB
/
index.js
File metadata and controls
66 lines (37 loc) · 1.28 KB
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
export function sum(first, second) {
return first + second;
}
export function isSpare(first, second) {
return ((first !== 10) && (first + second === 10)) ? 1 : 0;
}
export function isStrike(first) {
return (first === 10) ? 1 : 0;
}
export function curry(origin) {
const count = origin.length;
return function curry() {
return (count > arguments.length) ?
curry.bind(null, ...arguments) : origin.apply(this, arguments);
};
}
export function round(this_first, this_second, next_one, next_two) {
return this_first + this_second +
isSpare(this_first, this_second) * next_one +
isStrike(this_first, this_second) * (next_one + next_two);
}
const curry_round = curry( round );
const point = [ ], stack = [ ];
export function hit(count) {
var length = point.push( count ), curry;
if (length % 2) stack.push(curry = curry_round( count ));
for (let i = stack.length - 1; i >= 0; i--)
if ((curry !== stack[i]) && (stack[i] instanceof Function))
stack[i] = stack[i]( count );
if ((length % 2) && (count === 10)) hit( 0 );
}
export function scoreOf(index) {
return stack[ index ];
}
export function score() {
return stack.slice(0, 10).reduce( exports.sum );
}