Created
December 8, 2015 20:43
-
-
Save clive-bunting/f10cab7704df1cfc8367 to your computer and use it in GitHub Desktop.
Bonfire: Pairwise
This file contains hidden or 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
| // Bonfire: Pairwise | |
| // Author: @clive-bunting | |
| // Challenge: http://www.freecodecamp.com/challenges/bonfire-pairwise | |
| // Learn to Code at Free Code Camp (www.freecodecamp.com) | |
| function pairwise(arr, arg) { | |
| var used = []; | |
| for (var i = 0; i < arr.length - 1; i++) { | |
| if (used.indexOf(i) === -1) { | |
| for (var j = i + 1; j < arr.length; j++) { | |
| if (used.indexOf(j) === -1) { | |
| if (arr[i] + arr[j] === arg) { | |
| used.push(i, j); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| var sum = 0; | |
| used.forEach(function(x) { sum += x; }); | |
| return sum; | |
| } | |
| pairwise([1,4,2,3,0,5], 7); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment