Given two arrays, the goal is to determine if any two numbers in the array add up to 9.
The function should return either true or false.
For example:
array1 [1, 2, 4, 9] has no pair that sums up to 9, so it returns false
array2 [1, 2, 4, 5] does have a pair (4 and 5) that adds up to 9, so it returns true
To achieve this, you can use the following JavaScript function:
function hasPairWithSum(arr, sum) {
var len = arr.length;
for(var i=0; i<len-1; i++) {
for(var j=i+1; j<len; j++) {
if (arr[i] + arr[j] === sum)
return true;
}
}
return false;
}
console.log(hasPairWithSum([1, 2, 4, 9], 9));
console.log(hasPairWithSum([1, 2, 4, 5], 9));
If you're looking for an alternative approach to solving this problem, feel free to experiment further!