let arrayInput = [3, 34, 4, 12, 5, 2];
// function to find two values in inputArray that equal target
function findPairForSum(integers, target) {
let result = [];
var subtree = (givenArray, el2Eliminate) => {
let copy = [];
givenArray.forEach( (el, i, ar) => {
if (i !== el2Eliminate) {
copy.push(el);
}
});
return copy;
}
integers.forEach( (el, i, ar) => {
let eliminate = i;
let choppedArray = subtree(ar, i);
choppedArray.forEach( (el, i, ar) => {
if (eliminate + el == target) {
result.push(eliminate);
result.push(el);
return result;
}
});
});
return result;
}
// test 1
var pair = findPairForSum(arrayInput, 9);
console.log(`9 = ${pair}`); // --> [4, 5]
// test 2
pair = findPairForSum(arrayInput, 14);
console.log(`14 = ${pair}`); // --> [12, 2]
// test 3
pair = findPairForSum(arrayInput, 99);
console.log(`99 = ${pair}`); // --> [,]Tuesday, June 27, 2017
Functional Array Processing
Write a function that will search an array of integers for two values that equal 2nd argument.