Created
October 29, 2020 15:11
-
-
Save jchiatt/ecc765883e40f2814eebc6a42954cf90 to your computer and use it in GitHub Desktop.
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 twoNumberSum(array, targetSum) { | |
if ( !Array.isArray(array) || !array.length ) throw new Error('You must provide an array of values.'); | |
const result = []; | |
const numberMap = {}; | |
for ( let i = 0; i < array.length; i++ ) { | |
const diff = targetSum - array[i]; | |
console.log(diff) | |
if ( numberMap[diff] ) { | |
result.push(array[i], diff); | |
break; | |
} | |
numberMap[array[i]] = true; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment