Once you’ve sorted an array, you can leverage the index to retrieve the highest and lowest values.
Example
const points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a – b}); // now points[0] contains the lowest value // and points[points.length-1] contains the highest value |
Example
const points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return b – a}); // now points[0] contains the highest value // and points[points.length-1] contains the lowest value |
NOTE: Sorting the entire array is an inefficient approach if your objective is solely to determine the highest or lowest value. |