JavaScript - Given a list of integers, return the first two values that adds up to form sum. sum = x + y
Bug

The implementation doesn't work well with duplicate numbers:


console.log(sum_pairs([10, 5, 5, 3, 7, 4], 10));
[ 3, 7 ]



That output should be [ 5, 5 ].

Also,
for sum_pairs([10, 5, 2, 3, 7, 5], 8) I would have expected [ 5, 3 ] instead of [ 3, 5 ] as the output.
You didn't mention that results should be sorted.

The slowness

I see several reasons for the slowness:


The implementation is quadratic, using a nested loop
The implementation uses several expensive operations, such as slice and splice of arrays


Also, the implementation is extremely complicated and hard to follow,
so there might be other causes too.

A simpler solution is possible if you track the elements seen so far in an object, and check if target - current was seen or not.

var sum_pairs = function(ints, s) {
  var seen = {};

  for (let i = 0; i &lt; ints.length; i++) {
    let num = ints[i];
    if (seen[s - num]) {
      return [s - num, num];
    }
    seen[num] = true;
  }
}