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:
constwhen=(when=()=>false,fn=i=>i)=>(v,idx,arr)=>when(v,idx,arr)?(typeoffn==="function"?fn(v,idx,arr):fn):v;constreverseString=str=>Array.from(str).reverse().join("");constreverseWords=({inString="",ofLength=0}={})=>inString.split("").map(when(w=>w.length>=ofLength,reverseString)).join("");constgnirtS=reverseWords({inString:"one two three four",ofLength:5});
This one was fun! I decided to approach it from a slightly different angle by building a general-purpose
whenfunction 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:
Gist (w/ some tests): gist.github.com/kerrishotts/ac0f30...