Skip to content

Instantly share code, notes, and snippets.

View Constantiner's full-sized avatar
:octocat:
Glory to mankind!

Konstantin Kovalev Constantiner

:octocat:
Glory to mankind!
View GitHub Profile
@Constantiner
Constantiner / sum-pairs-extra-loop.js
Last active September 10, 2023 18:57
Test description
const getPairs = (arr, x) => arr.reduce(({pairs, subarray}, element) => ({pairs,
subarray: (subarray.forEach(elem => pairs.push([element, elem])), subarray.slice(1))}),
{pairs: [], subarray: arr.slice(1)}).pairs.filter(([a, b]) => a + b === x);
const arr = [ 3, 4, 5, -2, 10, 11, 12, -1, 0, 7, 8 ],
x = 10;
console.log(getPairs(arr, x));
const getPairs = (arr, x) => arr.reduce(({pairs, subarray}, element) => ({pairs,
subarray: (subarray.forEach(elem => element + elem === x ? pairs.push([element, elem]) : elem), subarray.slice(1))}),
{pairs: [], subarray: arr.slice(1)}).pairs;
const arr = [ 3, 4, 5, -2, 10, 11, 12, -1, 0, 7, 8 ],
x = 10;
console.log(getPairs(arr, x));