forked from makersacademy/bowling-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscorecard.js
More file actions
58 lines (50 loc) · 1.8 KB
/
scorecard.js
File metadata and controls
58 lines (50 loc) · 1.8 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
class Scorecard {
constructor() {
this.frames = [];
this.basicScore = 0;
this.strikeBonusScore = 0;
this.spareBonusScore = 0;
}
addFrame(roll1, roll2, roll3) { // it pushes each frame in the array
return this.frames.push([roll1, roll2, roll3]);
}
calculateBasicScore() { // it calculates the basic score
this.frames.map((frame) => {
const frameBasicScore = frame[0] + frame[1] + frame[2];
this.basicScore += frameBasicScore;
});
}
calculateStrikeBonuses() { // it adds the score of the next frame to the strikeBonusScore
for (let i = 0; i < this.frames.length - 1; i++) {
const currentArray = this.frames[i];
const nextArray = this.frames[i + 1];
if (currentArray[0] === 10) {
const sum = nextArray[0] + nextArray[1];
this.strikeBonusScore += sum;
}
}
}
calculateSpareBonuses() { // it adds the score of the first roll of the next frame to the spareBonusScore
for (let i = 0; i < this.frames.length - 1; i++) {
const currentArray = this.frames[i];
const nextArray = this.frames[i + 1];
if ((currentArray[0] + currentArray[1] === 10) && (currentArray[0] !== 10)) {
this.spareBonusScore += nextArray[0];
}
}
}
checkPerfectGame() { // it checks if all the first elements in the arrays are 10
const areAllStrikes = this.frames.every((subArray) => {
return subArray[0] === 10;
});
return areAllStrikes;
}
getTotalScore() { // it returns 300 if the frames are 12 and areAllStrikes is true
return (this.checkPerfectGame() && this.frames.length === 12) ?
300 : this.basicScore + this.strikeBonusScore + this.spareBonusScore;
}
getFrames() { // it returns the frames array just for checking
return this.frames;
}
};
module.exports = Scorecard;