Skip to content

Instantly share code, notes, and snippets.

@clive-bunting
Created December 8, 2015 20:43
Show Gist options
  • Select an option

  • Save clive-bunting/f10cab7704df1cfc8367 to your computer and use it in GitHub Desktop.

Select an option

Save clive-bunting/f10cab7704df1cfc8367 to your computer and use it in GitHub Desktop.
Bonfire: Pairwise
// 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