const findSum = (arr, sum) =>
  arr.some((set => n => set.has(n) || !set.add(sum - n))(new Set));



// 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);  
  });
  
  it('works correctly when the given array is unordered', () => {
  expect(findSum([4, 2, 4, 1], 8)).toEqual(true);
  expect(findSum([9, 2, 7, 1], 3)).toEqual(true);
  });
});
View Compiled
Run Pen

External CSS

  1. https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.css

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.js
  2. https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine-html.js
  3. https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/boot.js