DEV Community

Discussion on: Daily Challenge #34 - WeIrD StRiNg CaSe

Collapse
 
kerrishotts profile image
Kerri Shotts

Here's mine -- I'll admit that it took a little to recognize that the index was supposed to be word-based, not for the entire string. But that's what test cases are for! :-)

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(" ");

Gist: gist.github.com/kerrishotts/e3a213...