Skip to content

Instantly share code, notes, and snippets.

@rgaidot
Last active March 4, 2022 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgaidot/f0b988101b064912e3338aa15d2733de to your computer and use it in GitHub Desktop.
Save rgaidot/f0b988101b064912e3338aa15d2733de to your computer and use it in GitHub Desktop.
pairSum in javascript
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