I want to go though an array and check if any two numbers would sum to the target
value. Here’s my code:
function findPairForSum(arr, target) {
var pairSums = []
var newArr = []
for (var i = 0; i < arr.length; i++) {
if (arr[i] < target) {
newArr.push(arr[i])
}
}
// the above loop is to get rid of values that can't possibly be sumed to target (i.e. numbers larger than target)
//For the next loop, let's assume newArr[i] is 3
for (var k = 0; k < newArr.length; k++) {
var otherPair = target - newArr[i] // 6 = 9 - 3
if (newArr.includes(otherPair)) { // if newArr has 6
pairSums.push(newArr[i], otherPair) // push [3,6] to pairSum array
}
}
return pairSums
}
findPairForSum([3, 34, 4, 12, 5, 2], 9);
Can anyone tell me what I did wrong? It just returns an empty array. Thanks!