Skip to content

Commit f1f98a2

Browse files
committed
two sum: first day
1 parent 72dc07e commit f1f98a2

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

two_sum/main.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function twoSum(numbers, target) {
2+
let result;
3+
for(let i = 0; i < numbers.length - 1; i++) {
4+
let sub = target - numbers[i];
5+
for(let j = i + 1; j < numbers.length; j++) {
6+
if(sub === numbers[j]) {
7+
result = [numbers[i], numbers[j]];
8+
}
9+
}
10+
}
11+
return result;
12+
}
13+
14+
module.exports = twoSum;

two_sum/main.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const twoSum = require('./main');
2+
3+
test('the array of [1,2] and the parameter 3 returns [1,2]', () => {
4+
expect(twoSum([1,2], 3)).toStrictEqual([1,2]);
5+
});
6+
7+
test('the array of [2,2,1] and the parameter 4 returns [2,2]', () => {
8+
expect(twoSum([2,2,1], 4)).toStrictEqual([2,2]);
9+
});
10+
11+
test('the array of [1,5,3] and the parameter 4 returns [1,3]', () => {
12+
expect(twoSum([1,5,3], 4)).toStrictEqual([1,3]);
13+
});
14+
15+
test('the array of [5,3,1] and the parameter 4 returns [3,1]', () => {
16+
expect(twoSum([5,3,1], 4)).toStrictEqual([3,1]);
17+
});
18+
19+
test('the array of [1234,5678,9012] and the parameter 14690 returns [5678, 9012]', () => {
20+
expect(twoSum([1234,5678,9012], 14690)).toStrictEqual([5678,9012]);
21+
});
22+
23+
test('the array of [1,2,3] and the parameter 4 returns [1,3]', () => {
24+
expect(twoSum([1,2,3], 4)).toStrictEqual([1,3]);
25+
});

0 commit comments

Comments
 (0)