Calculating the score based on timing
This is done in the recordSpeedRecords()
method. It gets the records by getting the local storage item with the key records from the local storage. Then, we get the elapsedTime
and elapsedDiff
reactive property values and push them into the records
array.
Next, we sort the records with the sort()
method. This time, we are not sorting the items randomly. Rather, we are sorting them by the elapsedDiff
reactive property's timespan, which is measured in milliseconds. We pass in a callback with the a
and b
parameters, which are the previous and current array entries, respectively, and we return the difference between them. This way, if it returns a negative number or 0, then the order between them is unchanged. Otherwise, we switch the order. Then, we call slice
with the first and last index to include it in the returned array that we assigned to the sortedRecords
constant. The slice()
method returns an array with the item in the first index...