Created
April 18, 2015 16:12
-
-
Save crisberrios/842f7c6cacba0509ae13 to your computer and use it in GitHub Desktop.
pairwise
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 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