I am looking to create a function that can identify the largest pair sum in an unordered sequence of numbers.
largestPairSum([10, 14, 2, 23, 19]) --> 42 (sum of 23 and 19)
largestPairSum([99, 2, 2, 23, 19]) --> 122 (sum of 99 and 23)
largestPairSum([-10,-20,-30,-40]) --> -30 (sum of -10 and -20)
My Attempt:
function largestPairSum(numbers)
{
let counter = 0;
let orderedNumbers = numbers.sort();
if (orderedNumbers[0] === -orderedNumbers[0]) {
orderedNumbers.reverse();
counter = orderedNumbers[0] + orderedNumbers[1];
}
else {
counter = orderedNumbers[-1] + orderedNumbers[-2];
}
return counter;
}
Upon running the function, it returns 'NaN'. I tried checking the type using:
console.log(typeof(orderedNumbers[0]));
The type was shown as 'number', so I'm uncertain about where the issue lies. Thank you for your assistance!