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 permutations(set){ | |
| if (set.length == 0) return []; | |
| if (set.length == 1) return set; | |
| var values = set.filter((value, index) => set.lastIndexOf(value) == index); | |
| return values.reduce((result, value) => { | |
| var withoutCurrent = values.filter(x => x !== value); | |
| var permutationsStartingWithCurrent = permutations(withoutCurrent).map(x => [value].concat(x)); | |
| return result.concat(permutationsStartingWithCurrent); |
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 createPad(key, length){ | |
| var index = 0; | |
| var output = ""; | |
| while (index < length){ | |
| var k = key.charCodeAt(index % key.length); | |
| var charCode = ((((k * k) ^ 101) << 4) - index) % 256; | |
| output += String.fromCharCode(charCode); | |
| index++; |
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(number){ | |
| switch (true){ | |
| // Non-integers are not prime | |
| case !Number.isInteger(number): return false; | |
| // Negatives, 0, and 1 are not prime | |
| case number < 2: return false; | |
| // Single-digit prime factor checks to reduce looping | |
| case number % 2 == 0: return number == 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
| function hash(message){ | |
| // Seed each output character with the first 16 prime numbers | |
| var seed = [ | |
| 2, 3, 5, 7, | |
| 11, 13, 17, 19, | |
| 23, 29, 31, 37, | |
| 41, 43, 47, 53 | |
| ]; | |
| // Convert each message character into its ASCII character code |
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
| using System; | |
| namespace StackImplementation | |
| { | |
| public interface IStack<T> | |
| { | |
| int Depth(); | |
| bool IsEmpty(); |
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
| // Convert a decimal number to a string of Roman numerals | |
| function toRomanNumerals(number){ | |
| var repeat = (times, str) => Array(times).fill(str).join(""); | |
| return repeat(number, "I") | |
| // 1e1 | |
| .replace(new RegExp(repeat(10, "I"), "gi"), "X") // 10 | |
| .replace(new RegExp(repeat(9, "I"), "gi"), "IX") // 9 | |
| .replace(new RegExp(repeat(5, "I"), "gi"), "V") // 5 | |
| .replace(new RegExp(repeat(4, "I"), "gi"), "IV") // 4 |
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
| // On the first tab, we need to hijack any links that would open in a new tab | |
| // and open them with JavaScript so that we can store a reference to the newly opened tab | |
| // Vanilla JavaScript | |
| Array.from(document.querySelectorAll("a[target='_blank']")).forEach(function(a){ | |
| a.addEventListener("click", function(e){ | |
| e.preventDefault(); | |
| e.stopPropagation(); | |
| window.newTabReference = window.open(a.attributes.href, "_blank"); |
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
| // Runs <fn> immediately if not run in the last <ms> milliseconds | |
| // Skips execution of <fn> otherwise | |
| function debounce(fn, ms) { | |
| var lastRun = 0; | |
| return function () { | |
| var now = new Date().getTime(); | |
| if (now - ms >= lastRun) { | |
| lastRun = now; |
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
| private T TryCatchWrap<T>(Func<T> action, T defaultValue) | |
| { | |
| try | |
| { | |
| return action(); | |
| } | |
| catch (Exception e) | |
| { | |
| // Log the exception somewhere | |
| return defaultValue; |
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
| // Factory Function pattern of object creation | |
| function newUser(id, first, last){ | |
| // Validation of inputs happens in only one place - the constructor | |
| if (typeof id !== "number") throw new Error("Invalid ID"); | |
| if (typeof first !== "string") throw new Error("Invalid FirstName"); | |
| if (typeof last !== "string") throw new Error("Invalid LastName"); | |
| return { | |
| // Getters return the values used to create the object | |
| get ID(){ return id }, |