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 formatDate = (datestring) => { | |
| const seconds = Math.floor((new Date() - new Date(datestring)) / 1000); | |
| let interval = seconds / 31536000; | |
| if (interval > 1) return Math.floor(interval) + "Y"; | |
| interval = seconds / 2592000; | |
| if (interval > 1) return Math.floor(interval) + "M"; | |
| interval = seconds / 86400; | |
| if (interval > 1) return Math.floor(interval) + "d"; | |
| interval = seconds / 3600; | |
| if (interval > 1) return Math.floor(interval) + "h"; |
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 captilize = ([fL, ...rest]) => fL.toUpperCase()+rest.join(""); |
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
| String.prototype.mySubstring = function(start, end) { | |
| let substr = ''; | |
| for (i = (start || 0); i <= ((end || this.length) - 1); i++) { | |
| substr += this[i]; | |
| } | |
| return substr; | |
| } | |
| console.log("hello world!".mySubstring(1, 5)); // => ello |
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
| function isPrime(num) { | |
| if (num > 2 && num % 2 === 0) return false; | |
| for (var i = 3; i < Math.sqrt(num); i += 2) { | |
| if (num % i === 0) return false; | |
| } | |
| return num > 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 arr = [ [ '8', '0', '5', '7', '9' ], [ '1', '2', '4' ] ] | |
| const output = arr.reduce((a, b) => { | |
| let ret = []; | |
| a.forEach(i => b.forEach(j => ret.push(i + j))); | |
| return ret; | |
| }); | |
| // output = [ '81', '82', '84', '01', '02', '04', '51', '52', '54', '71', '72', '74', '91', '92', '94' ]; |
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) => (Array.from(Array(n), (_, i) => i + 1)).reduce((a, b) => a * 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
| //Based on https://stackoverflow.com/a/42531964 | |
| function combinations(array, length) { | |
| const arr = new Array(1 << array.length).fill().map( | |
| (e1, i) => array.filter((e2, j) => i & 1 << j)); | |
| return length ? arr.filter(a => a.length == length) : arr; | |
| } | |
| //console.log(combinations([1, 2, 3, 8, 10], 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 random = (i,j) => (Math.floor(Math.random() * (j - i)) + 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
| //Based on https://stackoverflow.com/a/46880431/607608 | |
| function getComb(set, val) { | |
| const res = []; | |
| function comb(set, k, partial, pref) { | |
| if (!partial) partial = []; | |
| for (const element in set) { | |
| if (k > 1) { | |
| const set_copy = set.slice(); | |
| set_copy.splice(element, 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
| function getRandom(arr, n) { | |
| var result = new Array(n), | |
| len = arr.length, | |
| taken = new Array(len); | |
| if (n > len) | |
| throw new RangeError("getRandom: more elements taken than available"); | |
| while (n--) { | |
| var x = Math.floor(Math.random() * len); | |
| result[n] = arr[x in taken ? taken[x] : x]; | |
| taken[x] = --len in taken ? taken[len] : len; |
NewerOlder