Pairwise Challenge Problem

Pairwise Challenge Problem
0

#1

Hi, I am curently working on the Pairwise challenge and I got a little bit stuck. Here is my code:

function pairwise(arr, arg) {
var sf=0;
var sf1=100;
if(arr.length==0){
return 0;
}
for(var i=0;i<arr.length-1;i++){
for(var j=i+1;j<arr.length;j++){
if(arr[i]+arr[j]==arg){
if(i+j<sf1){
sf1=i+j;
sf=i+j;
}else if(i+j==sf1){
sf=sf+i+j;
}
}
}
}
return sf;
}

pairwise([1, 4, 2, 3, 0, 5], 7);

Basically I’m trying to get all the first n-1 elements and check with the others if sum is equal with the value of the argument. I used two variables sf and sf1 for the sum of the indexes.In the first "if "we check if the sum is equal with arg. If it is, then we check if the sum of the indexes is lower than tha value of sf1(i used 100 to be sure that I would find a sum lower than sf1). If it is, both sf and sf1 are equal with i+j; I also used the else if where i+j==sf1 because I wanted that in case that I have another sum of elements equal to args and the sum of the indexes is equal with sf1 then I can add i+j to sf;

Basically I used the two variables and the else if for this case:

7 + 13 = 20 → Indices 0 + 3 = 3
9 + 11 = 20 → Indices 1 + 2 = 3
3 + 3 = 6 → Return 6