# Pairwise challenge can't pass one test

Pairwise challenge can't pass one test
0

#1

Hi,
My solution for Pairwise challenge passes all tests in Scratchpad(Firefox) but fails at `pairwise([1, 1, 1], 2)` in FCC.
Did I hit a bug, or am I missing something?

``````function pairwise(arr, arg) {
let used = new Array(arg).fill(false);
let pairsAndSum = [];
for (let i=0; i<arr.length; ++i) {
if (! used[i])
for (let j=i+1; j<arr.length; ++j)
if ((! used[j]) && (arr[i]+arr[j] == arg)) {
pairsAndSum.push([arr[i],arr[j],i+j]);
used[i] = used[j] = true;
break;
}
}
let uniques = [];
pairsAndSum.forEach(e => {
let i;
for (i=0; i<uniques.length; ++i)
if (uniques[i][0] == e[0] && uniques[i][1] == e[1])
if (uniques[i][2] > e[2]) {
uniques[i][2] = e[2];
break;
}
if (i == uniques.length)
uniques.push(e);
});

let sum=0;
for (let s of uniques)
sum += s[2];

return sum;
}
``````

#2

I took your solution and added some console.log statements to see various values of variables. Run the following code in the FCC site and look at the console compared to running it in Firefox and you will see where it is messing up. It has something to do with the use of ES6 syntax features such as let. To get your solution to pass, you just need to add to the first line of your code.

``````/*jshint esversion: 6 */
``````

Modified version with console.log statements below:

``````function pairwise(arr, arg) {
console.log('\n');
let used = new Array(arg).fill(false);
let pairsAndSum = [];
console.log('arr = ' + arr);
for (let i=0; i<arr.length; ++i) {
if (! used[i])
for (let j=i+1; j<arr.length; ++j)
if ((! used[j]) && (arr[i]+arr[j] == arg)) {
pairsAndSum.push([arr[i],arr[j],i+j]);
used[i] = used[j] = true;
break;
}
}
let uniques = [];
pairsAndSum.forEach(e => {
let i;
for (i=0; i<uniques.length; ++i) {
console.log('i='+i);
if (uniques[i][0] == e[0] && uniques[i][1] == e[1]) {
console.log('got here');
if (uniques[i][2] > e[2]) {

uniques[i][2] = e[2];
break;
}
}
}
if (i == uniques.length)
uniques.push(e);
});
console.log('uniques = ' + uniques);

let sum=0;
for (let s of uniques) {
console.log('s[2] = ' + s[2]);
sum += s[2];
console.log('sum = ' + sum);
}
return sum;
}
``````

FYI, the following code is never executed for any of the FCC tests:

``````        if (uniques[i][2] > e[2]) {
uniques[i][2] = e[2];
break;
}
``````

#3

Thanks, the code passed after adding the jshint line,

FYI, the following code is never executed for any of the FCC tests:

Yes, that code is part of the requirement If multiple pairs are possible that have the same numeric elements but different indices, return the smallest sum of indices. but the other requirement Once an element has been used, it cannot be reused to pair with another. eliminated all such possibilities among the FCC tests so it never gets executed.