# Quick Note: Finding Pairs of Numbers by Sum in JavaScript

Here's a little snippet if you ever need to find pairs of numbers in an array that matches a given sum.

I stumbled upon a Google Interviewing video on YouTube the other day where the challenge revolved around this and I thought I'd give it a shot in JavaScript. Maybe you'll find it useful.

` ````
const numbers = [8, 1, 2, 4, 4, 5, 2, 6, -1, 10, 5, 3]
function findNumberPairsBySum(numbers = [], targetSum = 8) {
return numbers.map((number, index, array) => {
const nextNumber = array[index + 1]
return nextNumber !== undefined && (number + nextNumber === targetSum) ? [number, nextNumber] : []
}).filter(pairs => pairs.length)
}
const numberPairs = findNumberPairsBySum(numbers, 5)
```

Peace ðŸ‘‹

Here's something I tried.

@smellycode That's a great implementation :) Cool use of

`.reduce`

. Also, if you for some reason want to avoid the`else`

, you could do something like...:)

Your content helped me a lot to take my doubts, thank you very much. kodi