Skip to content

Instantly share code, notes, and snippets.

@Gosilama
Last active April 29, 2020 18:33
Show Gist options
  • Save Gosilama/1091a170d96d76ef6f905fc503203b72 to your computer and use it in GitHub Desktop.
Save Gosilama/1091a170d96d76ef6f905fc503203b72 to your computer and use it in GitHub Desktop.
Given a list of numbers and a number k, return whether any two numbers from the list add up to k. For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
/**
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
*/
const arrayElementsSum = (arr, k) => {
// If k - index exists in array, return true
for (let i = 0; i < arr.length; i++) {
const diff = k - arr[i];
if (arr.includes(diff)) return true
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment