Hey, I have a question about line 4 (the if
condition) in this working code. Why does this work with array[index]
but not with the current item curr
? It seems like they’d be the same thing. Thanks!
function pairwise(arr, arg) {
return arr.reduce(function(acc, curr, index, array) {
for (var i = index + 1; i < array.length; i++) {
if (array[index] + array[i] === arg) {
acc += index + i;
array.splice(index, 1, NaN);
array.splice(i, 1, NaN);
}
}
return acc;
}, 0);
}
pairwise([1, 1, 1], 2); //should return 1.