Finding the first two numbers that sum to a given number Task aim: Given a list of integers and a single sum value, return the first two values (parse from the left please) in order of
  appearance that add up to form the sum.


I've been trying to make my implemented function below run faster (less than 7000ms) and taking into account records upwards of 10,000,000 items.

While it works for basic tests such as these:

sum_pairs( [1, 4, 8, 7, 3, 15], 8 ) == [1, 7]
sum_pairs( [1, -2, 3, 0, -6, 1], -6 ) == [0, -6]
sum_pairs( [20, -13, 40], 7 ) == undefined
sum_pairs( [1, 2, 3, 4, 1, 0], 2 ) == [1, 1] 
sum_pairs( [10, 5, 2, 3, 7, 5], 10 ) == [3, 7] 
sum_pairs( [4, -2, 3, 3, 4], 8 ) == [4, 4]
sum_pairs( [0, 2, 0], 0 ) == [0, 0]
sum_pairs( [5, 9, 13, -3], 10 ) == [13, -3]


it breaks with some other unseen tests (which presumably have some tricky 'edge cases' and thus, breaks my implementation which I suspect isn't an optimal solution.

It's been weeks now and I really would appreciate some guidance or signpost to some helpful resource to set me on the right track.

var sum_pairs = function (ints, s) {
    //your code here
    var i = 0,
    j = 0,
    len = ints.length,
    min = undefined,
    count = 0;

    for (i; i &lt; len; i++) {
        j = i + 1;
        while (j &lt; len) {
           if (s - ints[i] === ints[j]) {
               if ( !min || (j &lt; min[1]) ) {
                   min = [i, j];

                   // Check if worth continuing
                   close = (i+1===j);
                   if ( close ) break;
                }
            }
            j++;
        }
    }

    if (min !== undefined){
        min[0] = parseInt(ints[min[0]]);
        min[1] = parseInt(ints[min[1]]);
    }
    return min
}
Some issues with the code:


close isn't defined anywhere and will shoot up to the global scope.
Your variables are terribly named. Use names that make sense.
Suggesting a var per variable so they become portable. You can easily copy and paste without having to remove the comma and re-add the var.
I am not sure why you would need parseInt for the end part of your logic when ints appears to be an array of numbers.
Prefer function declaration (function foo(){}) over function expression (var foo = function(){}). Although variables and functions have the same namespace, function declarations get hoisted. They will always be available regardless if they appear later in the code.
JS uses camelCase convention.


For performance:


I suggest you keep a hash of first values so that whenever they come up again, like [ 1, 2, 3, 1, 6, 7], you can skip the second time it occurs, saving you a full run through the array.


Here's my take on it:

function sumPairs(numbers, sum){
  let first = null;
  let second = null;
  let firstHistory = {};

  for(let i = 0; i &lt; numbers.length; i++){
    first = numbers[i]

    // Skip if we've already seen this number as the first number
    if(firstHistory[first]) continue;

    // Remember that we've seen this number before
    firstHistory[first] = true;

    for(let j = 0; j &lt; numbers.length; j++){
      second = numbers[j];

      // Skip if we're on the same index
      if(i === j) continue;

      // Return once we've found it. Forget the rest.
      if(first + second === sum) return [first, second];
    }
  }

  // The only time the function ever reaches this point is when it never
  // returned (it never found the values). Functions return an implicit
  // undefined, which satisfies your "not found" case.
}


It doesn't cover one test case though. It sees [10, 5, 2, 3, 7, 5] as [5, 5] instead of [3, 7]. However, the definition of "earlier" in the condition is not clear. [5, 5] is found earlier than [3, 7] from an execution perspective.

And it might not be perfect, but the idea is there.