Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
.map((ch, idx) => transform(ch, idx & 1 ? TRANSFORMER.LOWER
: TRANSFORMER.UPPER ))
.join(""))
.join(" ");
const assert = (exprFn, expected, msg) => {
const actual = exprFn();
if (actual !== expected) {
throw new Error(`${msg}; got ${actual}; expected ${expected}`);
}
}
assert(() => weirdCase(""), "", "Empty");
assert(() => weirdCase(" "), " ", "Spaces");
assert(() => weirdCase("HELLO"), "HeLlO", "All Caps");
assert(() => weirdCase("hello"), "HeLlO", "All lower case");
assert(() => weirdCase("HellO"), "HeLlO", "Mixed case");
assert(() => weirdCase("Weird string case"), "WeIrD StRiNg CaSe", "Test Case");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment