const findSum = (arr, val) => {
for (let i = 0; i < arr.length; i++){
if (binarySearch(removeIndex(arr, i), val - arr[i])) {
return true;
}
};
return false;
};

const removeIndex = (arr, i) => {
return arr.slice(0, i).concat(arr.slice(i + 1, arr.length));
};

const binarySearch = (arr, val) => {
let start = 0;
let end = arr.length - 1;
let pivot = Math.floor(arr.length / 2);
while (start < end) {
if (val < arr[pivot]) {
end = pivot - 1;
} else if (val > arr[pivot]) {
start = pivot + 1;
};
pivot = Math.floor((start + end) / 2);
if (arr[pivot] === val) {
return true;
}
};
return false;
};

// Tests below
describe('findSum', function() {

it('returns TRUE if adding two numbers in an array yield the given value', () => {
expect(findSum([1, 2, 4, 4], 8)).toEqual(true);
});

it('returns FALSE if adding two numbers in an array does not yield the given value', () => {
expect(findSum([1, 2, 5, 9], 8)).toEqual(false);
});

it('does not count any value twice', () => {
expect(findSum([1, 2, 4, 9], 8)).toEqual(false);
expect(findSum([1, 2, 5, 6], 12)).toEqual(false);
});

it('can handle negative values', () => {
expect(findSum([-2, 1, 1, 4], 2)).toEqual(true);
expect(findSum([-3, -2, 1, 4], -5)).toEqual(true);
expect(findSum([-7, -3, -2, -1], -8)).toEqual(true);
});

it('can handle zero values', () => {
expect(findSum([-2, 0, 0, 1], 0)).toEqual(true);
expect(findSum([0, 0, 0, 0], 0)).toEqual(true);
expect(findSum([-1, 0, 1, 2], 0)).toEqual(true);
});
});