Given number array is - [10, 100, 20, 10, 0, 30, 40, 70, 50]; After I call sort number array becomes - [0, 10, 10, 20, 30, 40, 50, 70, 100]. I think the problem starts when I call sort. Latest() should return value of 50 not 100. I got stuck because got confused what specifically I need to change.
Can you copy/paste or screenshot on what methods you are calling on an instance of the HighScores
class?
Link to exercise? OR description of what you are trying to do?
As a reminder, sort() sorts the element of an array in-place i.e. the original array is sorted and mutated, not a copy of the array.
It seems like the issue is with the personalBest
method, which currently returns the highest score in the scores
array. However, after sorting the scores
array using the sort()
method, the highest score will be at the end of the array, not necessarily at the last index.
To fix this, you can modify the personalBest
method to first sort the scores
array, and then return the last element in the sorted array, which will be the highest score:
get personalBest() {
const sortedScores = this.scores.sort((a, b) => b - a);
return sortedScores[0];
}
With this modification, personalBest
will correctly return the highest score, regardless of whether the scores
array has been sorted or not.