Skip to content

Commit 39dd8e3

Browse files
committed
two sum: done but required refactor
1 parent f1f98a2 commit 39dd8e3

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

two_sum/main.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
function twoSum(numbers, target) {
2-
let result;
32
for(let i = 0; i < numbers.length - 1; i++) {
43
let sub = target - numbers[i];
54
for(let j = i + 1; j < numbers.length; j++) {
65
if(sub === numbers[j]) {
7-
result = [numbers[i], numbers[j]];
6+
return [i, j];
87
}
98
}
109
}
11-
return result;
1210
}
1311

1412
module.exports = twoSum;

two_sum/main.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
const twoSum = require('./main');
22

3-
test('the array of [1,2] and the parameter 3 returns [1,2]', () => {
4-
expect(twoSum([1,2], 3)).toStrictEqual([1,2]);
3+
test('the array of [1,2] and the parameter 3 returns [0,1]', () => {
4+
expect(twoSum([1,2], 3)).toStrictEqual([0,1]);
55
});
66

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]);
7+
test('the array of [2,2,1] and the parameter 4 returns [0,1]', () => {
8+
expect(twoSum([2,2,1], 4)).toStrictEqual([0,1]);
99
});
1010

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]);
11+
test('the array of [1,5,3] and the parameter 4 returns [0,2]', () => {
12+
expect(twoSum([1,5,3], 4)).toStrictEqual([0,2]);
1313
});
1414

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]);
15+
test('the array of [5,3,1] and the parameter 4 returns [1,2]', () => {
16+
expect(twoSum([5,3,1], 4)).toStrictEqual([1,2]);
1717
});
1818

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]);
19+
test('the array of [1234,5678,9012] and the parameter 14690 returns [1, 2]', () => {
20+
expect(twoSum([1234,5678,9012], 14690)).toStrictEqual([1,2]);
2121
});
2222

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]);
23+
test('the array of [1,2,3] and the parameter 4 returns [0,2]', () => {
24+
expect(twoSum([1,2,3], 4)).toStrictEqual([0,2]);
2525
});

0 commit comments

Comments
 (0)