"Skipping" when using a ternary operator?

function wave(str){
  return Array.from({length : str.length},
                    (v, i) => str.split("").map((e, k) =>  k == i ?  e.toUpperCase() : e).join(""))
}

wave(“two words”) returns
[“Two words”,“tWo words”,“twO words”,“two words”,“two Words”,“two wOrds”,“two woRds”,“two worDs”,“two wordS”]

I’m trying to tweak my code so that the value at index 3, “two words”, is skipped?
i.e tweak the function within the .map() to simply return nothing when " " is evaluated.

So I want “two words” excluded from the final array. I want to return
[“Two words”,“tWo words”,“twO words”, “two Words”,“two wOrds”,“two woRds”,“two worDs”,“two wordS”]

Map will always return an array of the same length as the one it was called on. You’ll need to add a call to filter, or something like that, to get rid of the items you don’t want.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.