My code is accepted, but I didn’t use a reduce method.
Could anyone tell me how I should change my following code and use .reduce() instead of a simple loop in a loop?
function pairwise(arr, arg) {
var count=0;
var indexArr=[];
function isInArray(value, array) {
return array.indexOf(value) > -1;
}
for (var i=0;i<arr.length;i++) {
// console.log('indexArr checking for i',i, indexArr);
for (var j=i+1;j<arr.length;j++) {
// console.log('indexArr checking for j', j , indexArr);
if(!isInArray(j, indexArr) && !isInArray(i, indexArr)) {
// console.log(arg, arr[i], arr[j], arr[i]+arr[j]);
if (arg-arr[i] === arr[j] ) {
count += (i+j);
indexArr.push(i,j);
// console.log('>>> i',i,'j',j, 'arr>>>', arr);
}
}
}
}
return count;
}
pairwise([0, 0, 0, 0, 1, 1], 1);