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 once = fn => ((...a) => { | |
| let executed = false; | |
| return (...a) => { | |
| if (!executed) { | |
| executed = true; | |
| return fn(...a); | |
| } | |
| }; | |
| })(); |
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 gcd = (a, b) => a ? gcd(b % a, a) : b; | |
| const lcm = (a, b) => a * b / gcd(a, b); | |
| [1, 2, 3, 4, 5].reduce(lcm); // Returns 60 |
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 add = n => { | |
| const f = x => add(n + x); | |
| f.valueOf = () => n; | |
| return f; | |
| } | |
| // Other Variants: | |
| function add(n) { | |
| var f = function(x) { return add(n+x); }; | |
| f.valueOf = function() { return n; }; |
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 longestSlideDown = p => p.reduceRight((x, e) => e.map((v, i) => v + Math.max(x[i], x[i + 1])))[0]; | |
| // https://www.codewars.com/kata/551f23362ff852e2ab000037 |
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 factorial = n => { | |
| if (!n) return '1' | |
| let i, next, carret; | |
| const result = n.toString().split``.reverse().map(Number); | |
| while (--n) { | |
| i = carret = 0; | |
| while ((next = result[i++]) !== undefined || carret) { | |
| carret += n * (next || 0); | |
| result[i - 1] = carret % 10; |
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 multiply = (a, b) => { | |
| const stack = []; | |
| a = a.split``.reverse(); | |
| b = b.split``.reverse(); | |
| for (let i = 0, la = a.length; i < la; i++) { | |
| for (let j = 0, lb = b.length; j < lb; j++) { | |
| const m = a[i] * b[j]; | |
| const s = stack[i + j]; | |
| stack[i + j] = s ? s + m : m; |
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 getPascalsTriangleRow = n => { | |
| const a = []; | |
| let x = 0; | |
| for (let i = 0; i < n; i++) { | |
| x = a.length - i; | |
| for (let j = 0; j < i + 1; j++) | |
| (j === 0 || j === i) ? a.push(1) : | |
| a.push(a[x + j] + a[x + j - 1]); | |
| } |
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 count = n => Math.floor((ln(2 * Math.PI * n) / 2 + n * (ln(n) - 1)) / ln(10)) + 1; | |
| const ln = x => Math.log(x) / Math.log(Math.E); |
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 calc = s => run(s.replace(/\s+/g, '').split``); | |
| const run = s => { | |
| let result = term(s); | |
| while (peek(s) == '+' || peek(s) == '-') { | |
| if (get(s) == '+') result += term(s); | |
| else result -= term(s); | |
| } | |
| return result; | |
| }; |
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 justify = (s, length) => { | |
| s = s.replace(/\n/g, ' ') | |
| s = s.split` `; | |
| const res = []; | |
| let line = []; | |
| let words = 0; | |
| for (let i = 0; i < s.length; i++) { | |
| if (words + line.length - 1 + s[i].length < length) { | |
| line.push(s[i]); |