-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (30 loc) · 762 Bytes
/
index.js
File metadata and controls
35 lines (30 loc) · 762 Bytes
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
const score = ([first, second, third, ...rest]) => {
// strike
if (first === 10 && second !== undefined && third !== undefined) {
return first + second + third + score([second, third, ...rest]);
}
// spare
if (first + second === 10) {
return first + second + third + score([third, ...rest]);
}
// open frame
if (first + second < 10) {
return first + second + score([third, ...rest]);
}
return 0;
};
const parse = (string) =>
[...string].map((character, currentIndex, characters) => {
switch (character) {
case 'X':
return 10;
case '/':
return 10 - parseInt(characters[currentIndex - 1]);
default:
return parseInt(character);
}
});
module.exports = {
score,
parse,
};