loading...

re: Daily Challenge #15 - Stop gninnipS My sdroW! VIEW POST

FULL DISCUSSION
 

This one was fun! I decided to approach it from a slightly different angle by building a general-purpose when function that could be used to decide when to map to a value (vs return the original value) based on a condition... just for kicks :-)

Here's my go:

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 gnirtS = reverseWords({inString: "one two three four", ofLength: 5});

Gist (w/ some tests): gist.github.com/kerrishotts/ac0f30...

code of conduct - report abuse