Skip to content

Instantly share code, notes, and snippets.

@crisberrios
Created April 18, 2015 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crisberrios/842f7c6cacba0509ae13 to your computer and use it in GitHub Desktop.
Save crisberrios/842f7c6cacba0509ae13 to your computer and use it in GitHub Desktop.
pairwise
function pairwise(arr, arg) {
var cloneArr = arr.slice(0);
return pair = cloneArr
.map(function(i) {
return arg-i;
})
.map(function(i,index) {
var pairIndex = cloneArr.indexOf(i);
if ( cloneArr[index] !== null && pairIndex >= 0 && pairIndex !== index) {
cloneArr[index] = null;
cloneArr[pairIndex] = null;
return index+pairIndex;
} else {
return 0;
}
})
.reduce(function(a,b) {
return a+b;
},0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment