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 👋


926 2 19