Skip to content

Instantly share code, notes, and snippets.

@kerrishotts
kerrishotts / 2019-Sep-16.js
Created September 16, 2019 15:21
2019-Sep-16 Dev.to Challenge (Grade Book)
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;
@kerrishotts
kerrishotts / challenge52.js
Last active August 31, 2019 12:08
Dev.to Challenge # 52
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");
@kerrishotts
kerrishotts / aug312019.js
Created August 31, 2019 07:59
Dev.to Aug 31 2019 Faro
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 -- */
@kerrishotts
kerrishotts / 20190806.js
Created August 6, 2019 20:26
Dev.To 2019 Aug 6 Challenge
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)
@kerrishotts
kerrishotts / 20190803.js
Created August 3, 2019 20:14
Dev.to 2019-08-03 Daily Challenge: # of valid IPs in a given range
/**
* 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);
/**
@kerrishotts
kerrishotts / 20190803.js
Created August 3, 2019 20:14
Dev.to 2019-08-03 Daily Challenge: # of valid IPs in a given range
/**
* 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);
/**
@kerrishotts
kerrishotts / 20190722.js
Last active July 23, 2019 03:09
2019-07-22 Dev.to challenge
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
@kerrishotts
kerrishotts / 20190719.js
Last active July 20, 2019 05:25
2019-07-19 Dev.to challenge
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`)];
@kerrishotts
kerrishotts / 20190718.js
Created July 20, 2019 04:48
2019-07-18 Dev.to challenge
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 = [
@kerrishotts
kerrishotts / 20190715.js
Created July 16, 2019 02:27
2019-07-15 Dev.to challenge
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))