"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.

Just to be clear, you want the element at index 3 (the one that matches what is passed to the function) to not be in the final array? Or are you talking about the space between the words? Can you just show the exact the array you are wanting to achieve with wave("two words")?

1 Like

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.