I am trying to find the second largest value in an array but my code returns the second smallest value in an array. Any pointers as to where i'm missing it? The code is suppose to simply loop backwards through the array, matching any value that is less than the highest value and also "one less than" the highest value.
function getSecondLargest(nums) {
let sNums = nums.sort();
let max = sNums.length - 1;
for (let i = sNums.length; i > 0; i--) {
if (sNums[i] < max && (sNums[i] === (max - 1))) {
return sNums[i];
}
}
}
console.log(getSecondLargest([8,7,9,4,5,6,3,2.10,22,42,101]))