Created
October 14, 2022 10:33
-
-
Save diraneyya/08cb8c42e73c944ce970961eb2c8e375 to your computer and use it in GitHub Desktop.
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
// Challenge one: Add all the numbers in array | |
array.reduce((a, e) => a + e); | |
// Challenge two: Create a string with all the numbers stitched in one string | |
array.join('') | |
array.reduce((a, e) => `${a}${e}`) | |
array.reduce((a, e) => '' + a + e); | |
array.reduce((a, e) => a + e.toString(), ''); | |
array.reduce((a, e) => a + e, ''); | |
array.reduce((a, e) => a.toString() + e.toString()); | |
// Challenge three: Find the maximum/minimum number of all numbers | |
Math.min(...array); | |
Math.min.apply(null, array) | |
array.reduce((e1, e2) => (e1 < e2 ? e1 : e2)); | |
Math.max(...array); | |
array.reduce((e1, e2) => (e1 < e2 ? e1 : e2)); | |
// Challenge: Counting the zeros | |
array.filter(el => el === 0).length | |
// Challenge four: Find the median number of all numbers | |
[...array] | |
.sort((a, b) => a - b) | |
.reduce((a, e, i, arr) => { | |
if (Math.abs(i - (arr.length - 1) / 2) < 1) | |
return a + e / (1 << (1 - (array.length & 1))); | |
else return a; | |
}, 0); | |
// IF YOU DO NOT UNDERSTAND THE ABOVE TRY THIS BABY-STEP | |
[...array].sort((a, b) => a - b).reduce((a, e, i, arr) => { | |
if (Math.abs(i - (arr.length - 1) / 2) < 1) | |
console.log(`${e} is at the center of array at i=${i}`); | |
return 0; | |
}, 0); | |
console.log( | |
`the elements will have to be divided by ${1 << (1 - (array.length & 1))}` | |
); | |
// Arkadii's solution, genius, sorts and counts in one reduce sentence | |
array.reduce( | |
(res, el) => [ | |
// Incrementally sorting the array by spreading the values under and above el | |
[...res[0].filter(n => n <= el), el, ...res[0].filter(n => n > el)], | |
// Counting the elements in the array | |
++res[1], | |
// Adds the two middle elements and divides by 2 | |
(res[0][ Math.floor( (res[1] - 1) / 2 ) ] + res[0][ Math.ceil( (res[1]- 1) / 2 ) ]) / 2 ] , [ [] , 0, 0 ] ) [2] | |
// Challenge five: Find the longest streak of zeros found in the array | |
Math.max( | |
...array.reduce( | |
(a, e, i, arr) => { | |
const p = i > 0 ? arr[i - 1] : null; | |
if (e === 0) | |
if (p !== 0) return [Math.max(...a), 1]; | |
else return [a[0], a[1] + 1]; | |
else return a; | |
}, | |
[0, 0] | |
) | |
); | |
// GENIUS: ARKADII | |
array.reduce( | |
(res, e) => | |
e === 0 ? [++res[0], res[1]] : res[0] > res[1] ? [0, res[0]] : [0, res[1]], | |
[0, 0] | |
)[1]; | |
// Challenge six: Extract the length of all the streaks of zeros found in the array | |
array.reduce((a, e, i, arr) => { | |
const p = i > 0 ? arr[i - 1] : null; | |
if (e === 0) | |
if (p !== 0) { | |
a.push(1); | |
return a; | |
} else { | |
a[a.length - 1]++; | |
return a; | |
} | |
else return a; | |
}, []); | |
// GENIUS ARKADII | |
array.reduce( | |
(res, e, i, arr) => | |
arr[arr.length - 1 - i] === 0 | |
? [++res[0], res[1]] | |
: res[0] | |
? [0, [...res[1], res[0]]] | |
: [0, [...res[1]]], | |
[0, []] | |
)[1]; | |
// RIGHT VERSION | |
array.reduceRight( | |
(res, e, i, arr) => | |
arr[arr.length - 1 - i] === 0 | |
? [++res[0], res[1]] | |
: res[0] | |
? [0, [...res[1], res[0]]] | |
: [0, [...res[1]]], | |
[0, []] | |
)[1]; | |
// EQUIVALENT RIGHT VERSION? | |
array.reduce( | |
(res, e, i, arr) => | |
arr[arr.length - 1 - i] === 0 | |
? [++res[0], res[1]] | |
: res[0] | |
? [0, [...res[1], res[0]]] | |
: [0, [...res[1]]], | |
[0, []] | |
)[1]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment