Skip to content

Instantly share code, notes, and snippets.

@oliverbth05
Created August 27, 2018 12:55
Show Gist options
  • Save oliverbth05/8fd2cf91594135f9579ad865f26a40e6 to your computer and use it in GitHub Desktop.
Save oliverbth05/8fd2cf91594135f9579ad865f26a40e6 to your computer and use it in GitHub Desktop.
Array: Find sum of two numbers
function findSum(arr, sum) {
var i = 0;
var j = arr.length-1;
var complete = false;
while (complete === false) {
if(arr[i] + arr[j] === sum) {
complete = true;
return [arr[i], arr[j]];
}
else if (arr[i] + arr[j] < sum) {
i++
}
else if (arr[i] + arr[j] > sum) {
j--
}
if (i === j) {
complete = true
}
}
return 'No combination found'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment