Created
August 27, 2018 12:55
-
-
Save oliverbth05/8fd2cf91594135f9579ad865f26a40e6 to your computer and use it in GitHub Desktop.
Array: Find sum of two numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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