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 angle = (a, b, c ,d) => ~~((Math.atan2(d - b, c - a)) * 180 / Math.PI); |
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
| /** | |
| * [1,2,3].equals([1,2,3]); // true | |
| * [1,2,3].equals([1,2]); // false | |
| * [1,2,3].equals([1,2,4]); // false | |
| * [1,2,3].equals("123"); // false | |
| * Array.prototype.equals.call("123", "123"); // true | |
| * Array.prototype.equals.call("123", [1,2,3]); // false | |
| * [1,2,3].equals([1,2,{value: 3}], (x, y) => (x.value || x) === (y.value || y)); // true | |
| */ | |
| Array.prototype.equals = function (other, callback = (x, y) => (x === y)) { |
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 decodeMorse = morseCode => morseCode | |
| .trim() | |
| .split` ` | |
| .map(e => morseDictionary[e]) | |
| .join`` | |
| .replace(/\s+/g, ' '); | |
| const morseDictionary = { | |
| '...---...': 'SOS', | |
| '-.-.--': '!', |
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 regexDivisibleBy(n) { | |
| if (n==1) return '^[01]+$' // Special case | |
| let a = [] // List of nodes | |
| for (let i=0; i<n; i++) a[i] = {} // Node | |
| for (let i=0; i<n; i++) { | |
| a[i].id = i // Used for indentification below | |
| a[i][i*2%n] = '0' // Keys in nodes are id refs to other nodes | |
| a[i][(i*2+1)%n] = '1' // Values in nodes are the required chars to get to next node (the key) | |
| } | |
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 regexDivisibleBy = n => { | |
| if (n == 1) return '^(0|1)+$'; | |
| if (n == 2) return '^(0|1)+0$'; | |
| if (n == 3) return '^(0*(1(01*0)*1)*)*$'; | |
| if (n == 4) return '^(0|1)+00$'; | |
| if (n == 5) return '^(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))*1)+$'; | |
| if (n == 6) return '^(0|(11|1(00|011*0)(00|011*0)*1)(11|1(00|011*0)(00|011*0)*1)*0)+$'; | |
| if (n == 7) return '^(0|(10((0|11)(1|00))*(10|(0|11)01)|11)(01*0(0|101|1(1|00)((0|11)(1|00))*(10|(0|11)01)))*1)+$'; | |
| if (n == 8) return '^(0|1)+000$'; | |
| } |
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 determinant = m => { | |
| const l = m.length; | |
| if (l === 1) return m[0][0]; | |
| if (l === 2) return m[0][0] * m[1][1] - m[1][0] * m[0][1]; | |
| let result = 0; | |
| for (let i = 0; i < l; i++) { | |
| result += Math.pow(-1, i) * m[0][i] * determinant(minor(m, i, 0)); | |
| } | |
| 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 parseInt = string => { | |
| var n = 0, | |
| g = 0; | |
| function text2num(s) { | |
| const a = s.toString().split(/[\s-]+/); | |
| a.forEach(feach); | |
| return n + g; | |
| } | |
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 RomanNumerals = { | |
| toRoman: v => { | |
| let s = ''; | |
| RomanNumerals.numerals.forEach(n => { | |
| while (v >= n[1]) { | |
| s += n[0]; | |
| v -= n[1]; | |
| } | |
| }); | |
| return s; |
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 toChineseNumeral(num) { | |
| var numerals = { | |
| "-":"负", | |
| ".":"点", | |
| 0:"零", | |
| 1:"一", | |
| 2:"二", | |
| 3:"三", | |
| 4:"四", | |
| 5:"五", |
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 add() { | |
| var c = 1000000000; | |
| var args = Array.prototype.slice.call(arguments); | |
| var sum = 0; | |
| for (var i = 0; i < args.length; i++) sum += args[i] * c; | |
| return (sum / c).toString(); | |
| } |