function sumOfTwo(nums1, nums2, value) {
//set return variable, default to false.
let sumFound = false;
//loop through first array and find the difference required to get the value
for (let i = 0; i < nums1.length; i++){
//get the value needed to achieve 'value' with current item
let diff = value - nums1[i];
//check if the needed difference is included in the second array
if(nums2.includes(diff)){
//if the value is there, at least one sum exists, return
sumFound = true;
break;
}
}
return sumFound;
}
/**
* 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);
});
});
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, 45];
const value = 42;
// act
const result = sumOfTwo(nums1, nums2, value);
// log
console.log("result: ", result);
// assert
expect(result).toBe(false);
});
});