0
\$\begingroup\$

Your goal is to alternate uppercase and lowercase letters, while ignoring non-alphaneumeric characters.

Input => Output
abc => aBc OR AbC
a c => A c OR a C
abc123abc => AbC123aBc OR aBc123AbC

Shortest answer wins.

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

JavaScript, 116

s=>{x=!0;return s.split("").map(e=>{if(e.match(/[a-zA-Z]/))x=!x;return x?e.toUpperCase():e.toLowerCase()}).join("")}

Saved two bytes thanks to NTCG

\$\endgroup\$
2
  • \$\begingroup\$ x=!0 can return true save 2 bytes and if you use join("") => join`` save 2 bytes \$\endgroup\$
    – NTCG
    May 15, 2018 at 1:52
  • 1
    \$\begingroup\$ You can save some bytes with template tags like .join`` instead of .join() and also you can use the spread operator to spread a string into an array instead of s.split``. [...s]. Not nearly as good as the answer on the other question but l=(s,x)=>[...s].map(e=>e.match(/[a-zA-Z]/)?e[`to${(x=!x)?"Upp":"Low"}erCase`]():e).join`` \$\endgroup\$
    – pfg
    May 15, 2018 at 2:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.