Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.