Created
July 16, 2019 02:27
-
-
Save kerrishotts/ac0f30c811c9465d21febf8ccd6816e5 to your computer and use it in GitHub Desktop.
2019-07-15 Dev.to challenge
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)) | |
.join(" "); | |
const assert = (expr, msg = "Assertion failed") => { | |
if (!expr) { | |
throw new Error(msg); | |
} | |
} | |
assert(reverseWords({inString: "", ofLength: 5}) === ""); | |
assert(reverseWords({inString: "hi", ofLength: 5}) === "hi"); | |
assert(reverseWords({inString: "hi you", ofLength: 5}) === "hi you"); | |
assert(reverseWords({inString: "hi you", ofLength: 5}) === "hi you"); | |
assert(reverseWords({inString: "one two three four", ofLength: 5}) === "one two eerht four"); | |
assert(reverseWords({inString: "sixteen candles burn", ofLength: 5}) === "neetxis seldnac burn"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment