Last active
April 29, 2020 18:33
-
-
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.
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
/** | |
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