My Solution
Challenge: Pairwise
Code
function pairwise(arr, arg) {
// Return if the array is empty
if (arr.length === 0) return 0;
var result = [];
// Reducer function
const reduce = (accumulator, currentValue) => accumulator + currentValue;
// Looping through every element in the array
for(var i=0; i<=arr.length; i++) {
// Looping through every successive element in the array
for(var j=i+1; j<=arr.length; j++) {
// Checking if sum of the values at position i & j are equal to the 2nd argument
if(
arr[i] + arr[j] == arg &&
(result.indexOf(i)<0 && result.indexOf(j)<0)
) {
//Pushing the indexes in an array
result.push(i);
result.push(j);
}
}
}
// Returning the sum of the indexes that matched the condition
return result.reduce(reduce);
}
pairwise([], 100);
Explanation
The function uses two loops to make every possible combination.
First one loops through every element in the array
Second one loops through every element after the element in the first loop.
If the condition is met, the value of i & j i.e. the index values are pushed into an array.
Finally, the array is reduced to get the final answer.
Links
Array
Reduce Array
Link to the challenge: