I am trying to improve my javascript skills, I am stuck on a problem I am working through.
// Given an integer array, output all pairs that sum up to a specific value p.
var sum = function(array, value){
var solutionArray = [];
for (i = 0; i < array.length; i++){
for(j = 0; j < array.length; j++){
if(array[i] + array[j] === value){
solutionArray.push([array[i], array[j]]);
}
}
}
return solutionArray;
};
This 'works' but I can't filter for duplicates. I would like to only have one match instead of displaying multiple. If anyone has any suggestions I would appreciate it.
I have been experimenting adding an && case to my if statement. I haven't found the logic to make that work.
I have also been experimenting filtering through my array before I return it and after it completed both loops.
Here is a repl.it I made of the problem: http://repl.it/QIk/1