Skip to content

Instantly share code, notes, and snippets.

@kerrishotts
Created July 16, 2019 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerrishotts/ac0f30c811c9465d21febf8ccd6816e5 to your computer and use it in GitHub Desktop.
Save kerrishotts/ac0f30c811c9465d21febf8ccd6816e5 to your computer and use it in GitHub Desktop.
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))
.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