I don’t get it, why the solution from guides uses array to collect indices, and then reduces it to get the result.
Our task is to find number, sum of indices.
So we can declare accumulator variable, and gather result with some +=
lines.
Also I am not a fan of using includes here, I think it’s unnecessary.
I would like to get feedback on these thoughts and also suggest alternative solution:
function pairwise(arr, arg) {
//result accumulator
let sumOfIndices = 0;
//to track already used elems
let isNotUsed = arr.map(elem => true)
for (let i = 0; i < arr.length - 1; i++) {
if (isNotUsed[i]) {
for (let j = i + 1; j < arr.length; j++) {
if (isNotUsed[j] === true &&
arr[i] + arr[j] === arg) {
sumOfIndices += (i + j);
isNotUsed[i] = false;
isNotUsed[j] = false;
break;
}
}
}
}
return sumOfIndices;
}
pairwise([1,4,2,3,0,5], 7);