Created
February 23, 2015 23:06
-
-
Save dmi3y/b6bbc01c58d85375cdff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findSumPairs(arr, sum) { | |
'use strict'; | |
var | |
i = 0, | |
j = 0, | |
isum, | |
imatch, | |
out = [], | |
larr; | |
larr = arr.slice(0); // shallow copy (just in case) | |
for ( i = 0; i < larr.length; i++ ) { | |
for ( j = 0; j < larr.length; j++ ) { | |
if ( i !== j ) { | |
isum = larr[i] + larr[j]; | |
if ( isum === sum ) { | |
imatch = [larr[i], larr[j]]; | |
out.push(imatch); | |
larr.splice(i, 1); | |
larr.splice(j, 1); | |
break; | |
} | |
} | |
} | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment