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 searchTree = (fn) => (obj) => | |
| Array.isArray(obj) | |
| ? obj.length == 0 | |
| ? null | |
| : searchTree(fn)(obj[0]) || searchTree(fn)(obj.slice(1)) | |
| : fn( | |
| obj, | |
| () => searchTree(fn)(obj.children || []), | |
| () => obj | |
| ); |
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 barr = [ | |
| { id: 1, is: true }, | |
| [ | |
| { id: 2, is: false }, | |
| [ | |
| { id: 3, is: true }, | |
| { id: 4, is: false }, | |
| ], | |
| ], | |
| ]; |
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 foo = [1, [2, [3, 4]]]; | |
| // foo.flat(Infinity) | |
| function flatten(a) { | |
| return a.reduce((flat, i) => { | |
| if (Array.isArray(i)) { | |
| return flat.concat(flatten(i)); | |
| } | |
| return flat.concat(i); | |
| }, []); |
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 maccs = [ | |
| { | |
| name: 'cheese burger', | |
| type: [ | |
| { name: 'fries', types: [{ name: 'chilli' }, { name: 'chicken salt' }] }, | |
| { name: 'bacon', types: [{ name: 'crispy' }, { name: 'bbq' }] }, | |
| { name: 'sauce', types: [{ name: 'ranch' }, { name: 'soy' }] }, | |
| ], | |
| }, | |
| { |
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
| // https://crocoder.dev/blog/map-filter-reduce-exercises/ | |
| const input = [1, -4, 12, 0, -3, 29, -150]; | |
| input.filter((a) => a > 0).reduce((a, b) => a + b, 0); | |
| const input2 = [12, 46, 32, 64]; | |
| const sorted = input2.sort((a, b) => a - b); | |
| const res = sorted.reduce( |
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
| var memo = {}; | |
| const fib = (n) => { | |
| if (n <= 1) return 1; | |
| if (memo[n]) return memo[n]; | |
| memo[n] = fib(n - 1) + fib(n - 2) | |
| return memo[n]; | |
| }; | |
| fib(1); | |
| fib(2); |
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 names = ['a', 'b', 'bd', 'bc', 'f']; | |
| const b = names.sort((a, b) => { | |
| if (a < b) return -1; | |
| if (b < a) return 1; | |
| return 0; | |
| }); | |
| b; |
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 files: MyFile[] = [ | |
| { name: 'raz', size: 45, age: 4 }, | |
| { name: 'pop', size: 33, age: 6 }, | |
| { name: 'foo', size: 10, age: 9 }, | |
| { name: 'crackle', size: 21, age: 2 }, | |
| { name: 'baz', size: 22, age: 8 }, | |
| { name: 'bar', size: 15, age: 11 }, | |
| ]; | |
| interface MyFile { |
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 assert = (v) => { | |
| if (!v) { | |
| throw new Error('False'); | |
| } | |
| return true; | |
| }; | |
| const groupBy = (arr, fn) => | |
| arr | |
| .map(typeof fn === 'function' ? fn : (val) => val[fn]) |
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
| /** | |
| * Code recipe: Writing JavaScript groupBy() function | |
| */ | |
| // https://sebhastian.com/javascript-group-by/ | |
| const movies = [ | |
| { title: 'Sonic the Hedgehog', year: 2020 }, | |
| { title: 'Mulan', year: 2020 }, | |
| { title: 'Godzilla vs. Kong', year: 2021 }, |