This file contains 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 sum = (...nums) => nums.reduce((total, cur) => total + cur, 0); | |
const meanGrade = (...grades) => sum(...grades) / grades.length; | |
const letterForGrade = grade => { | |
const lastDigit = grade % 10; | |
const gradeLetter = Object.entries({90: "A", 80: "B", 70: "C", 60: "D", 0: "F"}) | |
.sort(([a], [b]) => a < b) | |
.find(([minGrade]) => grade >= minGrade) | |
[1] + ((lastDigit < 5) ? "-" : | |
(lastDigit > 5) ? "+" : ""); | |
return gradeLetter; |
This file contains 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 PYRAMID = "*"; | |
const AIR = "·"; | |
const pyramid = (n, {maxWidth = (n * 2) - 1, leftPadding = maxWidth / 2} = {}) => | |
Array.from({length: n}, (_, idx, __, {width = (( idx + 1 ) * 2) - 1} = {}) => | |
PYRAMID.repeat(width) | |
.padStart(idx + leftPadding + 1, AIR) | |
.padEnd(maxWidth, AIR) | |
).join("\n"); |
This file contains 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 split = arr => | |
[arr.slice(0, arr.length / 2), arr.slice(arr.length / 2)]; | |
const interleave = (a, b) => | |
a.reduce((shuffled, aIdx, idx) => (shuffled.push(aIdx, b[idx]), shuffled), []); | |
const faro = arr => interleave(...split(arr)); | |
/* -- Testing -- */ |
This file contains 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 TRANSFORMER = { | |
LOWER: String.prototype.toLowerCase, | |
UPPER: String.prototype.toUpperCase | |
}; | |
const transform = (v, which) => which.apply(v); | |
const weirdCase = str => | |
str.split(" ") | |
.map((part) => Array.from(part) |
This file contains 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
/** | |
* Converts an IP String to its integer representation | |
* | |
* @param ipString {string} An IP Address in the form a.b.c.d | |
* @return {number} The integer representation of the IP address | |
*/ | |
const convertIPStringToNumber = ipString => ipString.split(".") | |
.reduce((acc, part) => (acc << 8) | Number(part), 0); | |
/** |
This file contains 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
/** | |
* Converts an IP String to its integer representation | |
* | |
* @param ipString {string} An IP Address in the form a.b.c.d | |
* @return {number} The integer representation of the IP address | |
*/ | |
const convertIPStringToNumber = ipString => ipString.split(".") | |
.reduce((acc, part) => (acc << 8) | Number(part), 0); | |
/** |
This file contains 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 notEmpty = i => i !== ""; | |
const singularize = str => str.substr(0, str.length - 1); | |
const englishJoin = (str, part, idx, arr) => str + ((idx === arr.length - 1 && arr.length > 1) ? " and " : ( str && ", ")) + part; | |
const spellInterval = (seconds = 0) => | |
Object.entries({ | |
years: 365 * 24 * 60 * 60, | |
days: 24 * 60 * 60, | |
hours: 60 * 60, | |
minutes: 60, | |
seconds: 1 |
This file contains 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 numToText(n) { | |
if (n < 1 || n > 999 || n === undefined) { | |
throw new Error(`arg must be > 0 and < 1000`); | |
} | |
const ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; | |
const prefixes = ["thir", "four", "fif", "six", "seven", "eigh", "nine"]; | |
const tys = ["", "", "twenty", ...prefixes.map(prefix => `${prefix}ty`)]; | |
tys[4] = "forty"; | |
const oneTeens = [...ones, "ten", "eleven", "twelve", ...prefixes.map(prefix => `${prefix}teen`)]; |
This file contains 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 makeRun = (v, {ofLength = 1} = {}) => Array.from({length: ofLength}, () => v).join(""); | |
const hasTripleTrouble = (triple, double) => { | |
const [tripleStr, doubleStr] = [triple, double].map(v => String(v)); | |
return (Array.from({length: 10}, (_, idx) => idx) | |
.some(digit => (tripleStr.indexOf(makeRun(digit, {ofLength: 3})) > -1 | |
&& doubleStr.indexOf(makeRun(digit, {ofLength: 2})) > -1))); | |
} | |
const testCases = [ |
This file contains 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 when = (when = () => false, fn = i => i) => | |
(v, idx, arr) => when(v, idx, arr) | |
? (typeof fn === "function" ? fn(v, idx, arr) : fn) | |
: v; | |
const reverseString = str => Array.from(str).reverse().join(""); | |
const reverseWords = ({inString = "", ofLength = 0} = {}) => | |
inString.split(" ") | |
.map(when(w => w.length >= ofLength, reverseString)) |