Skip to content

Commit d15c850

Browse files
committed
Done with finding pair sum.
1 parent b152b6b commit d15c850

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

list/pair-number-sum/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @author: Chisimdi Damian Ezeanieto
3+
* @date: 22/01/2020
4+
*/
5+
6+
const pairNumberSum = (arr, k) => {
7+
const result = [];
8+
// Naive solution
9+
// Use two loops to loop through the array.
10+
let i = 0;
11+
while(i < arr.length) {
12+
let deficit = k - arr[i];
13+
let newArray = arr.slice(i);
14+
let findIndex = newArray.indexOf(deficit);
15+
if (findIndex > -1) {
16+
let findItem = newArray[findIndex];
17+
let pairs = [arr[i], findItem];
18+
result.push(pairs);
19+
}
20+
i++;
21+
}
22+
return result;
23+
}
24+
25+
module.exports = pairNumberSum;

list/pair-number-sum/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* @author: Chisimdi Damian Ezeanieto
3+
* @date: 22/01/2020
4+
*/
5+
const pairNumberSum = require('./index');
6+
test('pairNumberSum is a function', () => {
7+
expect(typeof pairNumberSum).toEqual('function');
8+
});
9+
test('pairNumberSum [8,2,54,65,23,7,45] to give [2,7,8,23,45,54,65]', () => {
10+
expect(pairNumberSum([1, 9, 11, 13, 2, 3, 7, 5], 12)).toEqual([[1, 11], [9,3], [7, 5]]);
11+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "jest --watch"
88
},
99
"repository": {
1010
"type": "git",

0 commit comments

Comments
 (0)