Last active
March 4, 2022 09:57
-
-
Save rgaidot/f0b988101b064912e3338aa15d2733de to your computer and use it in GitHub Desktop.
pairSum in javascript
This file contains hidden or 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
const pairSumExplain = (array: Array<number>, target: number): Array<Array<number>> => { | |
const length: number = array.length; | |
const found: Array<Array<number>> = []; | |
for(let i = 0; i < length; i++) { | |
for(let j = (i + 1); j < length; j++) { | |
if(target == array[i] + array[j]) found.push([array[i], array[j]]); | |
} | |
} | |
return found; | |
} | |
const pairSumBool = (array: Array<number>, target: number): boolean => { | |
const length: number = array.length; | |
for(let i = 0; i < length; i++) { | |
for(let j = (i + 1); j < length; j++) { | |
if(target == array[i] + array[j]) return true; | |
} | |
} | |
return false; | |
} | |
const array: Array<number> = [2, 7, -3, 1, 6, -2, 3, 10, 27, 8, 1, 10, 11, 4, 5]; | |
const target: number = 9; | |
const value: boolean = pairSumBool(array, target); | |
console.log(`return: ${value}`); | |
if(value) { | |
console.log('explain:'); | |
console.log(pairSumExplain(array, target)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment