-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 21 - Sum of 2.js
67 lines (52 loc) · 1.56 KB
/
Day 21 - Sum of 2.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
63
64
65
66
67
/* Sum of 2
https://scrimba.com/scrim/co40e47ae8fc3d510615205e2
DESCRIPTION:
You have two integer arrays, and b, and an integer target value v. Determine whether there is a pair of numbers, where
one is taken from a and the other from b that can be added together to get a sum of v. Return true if such a pair exists,
otherwise return false.
Example:
For a = [1,2,3], b = [10, 20, 30, 40] and v = 42, the output should be sumOfTwo(a,b,v) = true
Hints: hasOnwProperty()
*/
function sumOfTwo(nums1, nums2, value) {
for (let i = 0; i < nums1.length; i++) {
for (let j = 0; j < nums2.length; j++) {
if ((nums1[i] + nums2[j]) == value ) {
return true
}
}
}
return false
}
/* or (old solution):
function sumOfTwo(nums1, nums2, value) {
let result = 0
let bool = false
for (let i = 0; i < nums1.length; i++) {
for (let j = 0; j < nums2.length; j++) {
let add = nums1[i] + nums2[j]
if (add == value ) {
result = add
bool = true
}
}
}
return bool
}
/**
* Test Suite
*/
describe('sumOfTwo()', () => {
it('returns true if a value can be found that by adding one number from each list', () => {
// arrange
const nums1 = [1, 2, 3];
const nums2 = [10, 20, 30, 40];
const value = 42;
// act
const result = sumOfTwo(nums1, nums2, value);
// log
console.log("result: ", result);
// assert
expect(result).toBe(true);
});
});