Discovering pairs of distinct integers in an array that sum up to a target value can be challenging. If successful, you'll need to organize these pairs in ascending order within arrays. In case no such pairs exist, an empty array should be returned.
I attempted to solve this problem using a specific approach, but unfortunately, it didn't yield the desired results.
function findPairs(arr, target) {
let result = []
for (let i = 0; i <= arr.length - 1; i++) {
for (let j = i + 1; j < arr.length - 1; j++) {
if (arr[i] + arr[j] === target) {
result.unshift(arr[i], arr[j])
}
}
}
return new Array(result)
}
console.log(findPairs([3, 7, 8, 4, 5, 9], 12)) // [[3,9],[4,8],[5,7]]
console.log(findPairs([1, 2, 3, 4], 8)) // []