forked from makersacademy/bowling-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoreCard.js
46 lines (41 loc) · 1.09 KB
/
scoreCard.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
class ScoreCard {
constructor() {
this.frames = [];
}
addFrame(firstRoll, secondRoll) {
this.frames.push({
firstRoll: firstRoll,
secondRoll: secondRoll,
});
}
calculateScore() {
let score = 0;
for (let i = 0; i < this.frames.length; i++) {
const frame = this.frames[i];
score += frame.firstRoll + frame.secondRoll;
if (i < 9) {
// strike
if (frame.firstRoll === 10) {
score += this.frames[i + 1].firstRoll;
// consecutive strike
if (this.frames[i + 1].firstRoll === 10) {
score += this.frames[i + 2].firstRoll;
} else {
score += this.frames[i + 1].secondRoll;
}
// spares case
} else if (frame.firstRoll + frame.secondRoll === 10) {
score += this.frames[i + 1].firstRoll;
if (
this.frames[i + 1].firstRoll + this.frames[i + 1].secondRoll ===
10
) {
score += this.frames[i + 2].firstRoll;
}
}
}
}
return score;
}
}
module.exports = ScoreCard;