-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.twonumbersum.js
62 lines (58 loc) · 1.25 KB
/
1.twonumbersum.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Write a function that takes in a non-empty array of distinct integers and an
* integer representing a target sum. If any two numbers in the input array sum
* up to the target sum, the function should return them in an array, in any
* order. If no two numbers sum up to the target sum, the function should return
* an empty array.
* Note that the target sum has to be obtained by summing two different integers
* in the array; you can't add a single integer to itself in order to obtain the
* target sum.
* You can assume that there will be at most one pair of numbers summing up to
* the target sum.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* Check my solutions
*/
function twoNumberSum(array, targetSum) {
// Write your code here.
let obj = [];
array.map( (a) =>
{
array.map((b) => {
if(a + b === targetSum){
obj = [a, b];
}
else{ return; }
}
)
});
return obj;
}
function twoNumberSum2(array, targetSum) {
// Write your code here.
for(let key of array){
for(let v = 0; v <= array.length; v++){
if(key + array[v] === targetSum && key !== array[v]){
return [key, array[v]];
}
}
}
return [];
}
/*
Test:
- array= [3, 5, -4, 8, 11, 1, -1, 6]
- targetSum = 10
*/