I have an array of numbers: [20,40,60,60,20]. The task is to find pairs of two numbers whose sum is divisible by 60. In this array, there are 3 such pairs: (20,40), (40,20), and (60,60).
However, when I implemented the code to achieve this, it returned 4 instead of 3.
function countPairs (array) {
let count = 0;
for (let i = 0; i < array.length-1; i++) {
for (let j = 0; j < array.length-1; j++) {
let a = array[i];
let b = array[j];
if (checkPair(a, b) && notSameIndex(i, j)) {
count++;
}
}
}
return count;
}
function checkPair (a, b) {
return Number.isInteger((a + b)/60);
}
function notSameIndex (x, y) {
return x !== y ? true : false;
}
Can you spot what's causing the discrepancy in the result?