forked from AllAlgorithms/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2sum.js
More file actions
30 lines (27 loc) · 853 Bytes
/
2sum.js
File metadata and controls
30 lines (27 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// File name: 2sum.js
// Author: boasmarbun (https://github.com/boasmarbun)
// Date created: 02/10/2020
function twoSum (nums, target) {
var diff = 0;
for (var firstIndex = 0; firstIndex <= nums.length; firstIndex++) {
diff = target - nums[firstIndex];
var secondIndex = nums.indexOf(diff, firstIndex+1);
if(secondIndex != -1) {
return [firstIndex, secondIndex];
}
}
return [0];
};
// uncomment below for testing
// var testArray1 = [1,2,3,9]
// var target1 = 5
// var testArray2 = [0,3,4,0]
// var target2 = 0
// var testArray3 = [-2, -3, -7, -13, -6]
// var target3 = -8
// var testArray4 = []
// var target4 = 2
// console.log(twoSum(testArray1, target1))
// console.log(twoSum(testArray2, target2))
// console.log(twoSum(testArray3, target3))
// console.log(twoSum(testArray4, target4))