| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export class HighScores { |
| 7 | | constructor() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 7 | constructor(scores) { |
| 8 | this._scores = scores; |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | get scores() { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 12 | return this._scores; |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | get latest() { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 16 | // Edge Case: empty score list |
| 17 | if (this._scores.length === 0) { |
| 18 | return undefined; |
| 19 | } |
| 20 | return this._scores[this._scores.length - 1]; |
| 17 | 21 | } |
| 18 | 22 | |
| 19 | 23 | get personalBest() { |
| 20 | | throw new Error('Remove this line and implement the function'); |
| 24 | // Edge Case: empty score list |
| 25 | if (this._scores.length === 0) { |
| 26 | return undefined; |
| 27 | } |
| 28 | return Math.max(...this._scores); |
| 21 | 29 | } |
| 22 | 30 | |
| 23 | 31 | get personalTopThree() { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 32 | // Edge Case: empty score list |
| 33 | if (this._scores.length === 0) { |
| 34 | return []; |
| 35 | } |
| 36 | // Create a copy of the array and sort in descending order |
| 37 | const sortedScores = [...this._scores].sort((a, b) => b - a); |
| 38 | // Return the first three elements (or all if fewer than three) |
| 39 | return sortedScores.slice(0, 3); |
| 25 | 40 | } |
| 26 | 41 | } |
| 42 | |
| 43 | // Handled Edge Cases: empty score list, single score in list, duplicate scores, negative scores, list with fewer than three scores |