# Finding a pair that is divisible by some number in JavaScript

JavascriptWeb DevelopmentFront End Technology

We are required to write a JavaScript function that takes in an array of Numbers as the first argument, let's call it arr and a single number as the second argument, let's call it num.

The function should find all such pairs from the array where −

arr[i] + arr[j] = num, and
i < j

For example −

If the input array and the number is −

const arr = [1, 2, 3, 4, 5, 6];
const num = 4;

Then the output should be −

const output = [
[1, 3], [2, 6], [3, 5]
];

## Example

The code for this will be −

Live Demo

const arr = [1, 2, 3, 4, 5, 6];
const num = 4;
const divisibleSumPairs = (arr = [], num) => {
const res = [];
const { length } = arr;
for(let i = 0; i < length; i++){
for(let j = i + 1; j < length; j++){
const sum = arr[i] + arr[j];
if(sum % num === 0){
res.push([arr[i], arr[j]]);
}
}
}
return res;
};
console.log(divisibleSumPairs(arr, num));

## Output

And the output in the console will be −

[ [ 1, 3 ], [ 2, 6 ], [ 3, 5 ] ]