Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Help for Bardic Wizard

Source: https://codegolf.stackexchange.com/a/206808/9365

Original (287):

function s(){a="kdg by the dj,/nThe g care dg, I'm sure./nSo if cdg on the dj,/nThen I'm sure cdj g.".replace(/c|k/g,function(match){return(match=="c")?"she sells ":"She sells ";});c=a.replace(/d|g/g,function(match){return(match=="d")?"sea":"shells";});return (c.replace(/j/g,"shore"));}

1. Use shorter variable names (271: -16):

function s(){a="kdg by the dj,/nThe g care dg, I'm sure./nSo if cdg on the dj,/nThen I'm sure cdj g.".replace(/c|k/g,function(m){return(m=="c")?"she sells ":"She sells ";});c=a.replace(/d|g/g,function(m){return(m=="d")?"sea":"shells";});return (c.replace(/j/g,"shore"));}

https://tio.run/##dZDBbsIwDIbvPIWVC4lU2uu0KOLMGV4guCZrCUlJ2kqo8Oydx7SVCzf/8vd/ltza0WZMTddvxo95Pg0B@yYGyFJN1ohz7eB4g/6LoG6LKhx4cIA2cXYF7NYXyEOisgr7CM0JkHluv/Lhn@JtC64UZaLOWyRZ4f1cueLvqLyoKVE/JJ6MESjUVmQ2ZfI@g/gU@yXoh9Jo7KKq7@69qn6qyLKDhSx49n8BkLhYWnYwEhMJpfRjvoL5eYVeYQw5eip9dPKq9PwN

2. Use arrow functions (227: -44):

_=>{a="kdg by the dj,/nThe g care dg, I'm sure./nSo if cdg on the dj,/nThen I'm sure cdj g.".replace(/c|k/g,m=>(m=="c")?"she sells ":"She sells ");c=a.replace(/d|g/g,m=>(m=="d")?"sea":"shells");return (c.replace(/j/g,"shore"))}

https://tio.run/##VcsxD4IwEIbh3V9x6WKbIKxGUp2ddTf1OKtYWmyBxKi/HS8Oott9ufepzWASxkvbLYblmPR40OuH0eJaWTjeoTsTVHVW@D0fFtBE3jaD7byB1EfKC78LcDkBch/8X@@/FX9rsLnII7XOIMkCn9fCZo1ey0ZrgUJtRGKayLkEYiV201AlajPJ6ml/ZPWRZJiw557zSF0fPUicUM2EgxBJKPUab6AhSVXOMPgUHOUuWHlT5fgG

3. Remove return, braces and chain calls to replace (208: -19):

_=>"kdg by the dj,/nThe g care dg, I'm sure./nSo if cdg on the dj,/nThen I'm sure cdj g.".replace(/c|k/g,m=>(m=="c")?"she sells ":"She sells ").replace(/d|g/g,m=>(m=="d")?"sea":"shells").replace(/j/g,"shore")

https://tio.run/##Vcu7DoIwFIDh3ac46WKbIF2NpDg7425qqRUoLbRIYsK71xMHwe1cvr@Vs4wqNMN0mI8pinQTJelqA/c3TE8NdZtxd8XBgJIBd5PBZd9DfAWdc1d5aB6g0Hv3591P4bcFk5M86MFKpSlXS8dN1ouS9kIQRdiZREyjtjYCOZFqXdha1YvZVPW30hI5tmi3tEWIZx80YWkEAZGyYqe8i97q3HpDR1akDw

4. Use string literals [this also renders as expected ;)] (204: -4):

_=>`kdg by the dj, The g care dg, I'm sure. So if cdg on the dj, Then I'm sure cdj g.`.replace(/c|k/g,m=>(m=="c")?"she sells ":"She sells ").replace(/d|g/g,m=>(m=="d")?"sea":"shells").replace(/j/g,"shore")

https://tio.run/##TcuxDoIwEIDhnae4dLFNEFYjKc7OuEsttVIKhRZJTHj3enEQpsvdfb8RiwjSt@N8XE4x8HjnZd01Gh4fmF8KGpMmN5wapPC46hSuhx7C26ssqRy0T5Co3bDXw9/g04DO6syr0QqpaC7XLtdpz0vac04kYRcSsAzK2gDkTKptYVvVrHpXNb9KCeTYot1TgxDPzivC4gQcAmVFIt0QnFWZdZpOrIhf

5. Combine replacements into an object literal (177: -27):

_=>`kdg by the dj, The g care dg, I'm sure. So if cdg on the dj, Then I'm sure cdj g.`.replace(/c|k|d|g|j/g,m=>({c:"she sells ",k:"She sells ",d:"sea",g:"shells",j:"shore"}[m]))

https://tio.run/##TYtND4IgHMbvfor/uIQb6bXp8N7Zbq0lAVGIomBuLfrsRh2ap@ft92g2M8/dfZi2827xdDnTqmmFgssTppsEoUlyiKqAMxejIrDfdOAfTmZJbeF@BR5p26/p/s/EUYPKmszJwTAucc5DG0RQQeeKdLTCL14gH69eGuMBkbZA9SqKuEqGiPpRsUNEf611Er2P3SlNlxEoeJyWCbe9t0Zmxio8puXyAQ

6. Use an array instead of an object (158: -19):

_=>`123 by the 24, The 3 0are 23, I'm sure. So if 023 on the 24, Then I'm sure 024 3.`.replace(/\d/g,m=>["she sells ","She sells ","sea","shells","shore"][m])

https://tio.run/##TYuxDoIwFEV3vuKli5BgQepgQsrujBsSqVgBUyj0IYlfX4uDYXn3ntzzXmIRWJtunPfLySK3N55Vh4TB/QNzKyE5ht7FJYNYGIcshPOuB3wbSb1cQ/eE2Nl62NrD33HjERitqJGjErX0o@sjasKeZwVB94BSKQQSknwLKMV625V/RRtJyqIvAzsBB/SD1Kv1gFpJqnTjT0Fqvw

7. Don't have She sells and she sells separately (148: -10):

_=>`S012 by the 13, The 2 s0are 12, I'm sure. So if s012 on the 13, Then I'm sure s013 2.`.replace(/\d/g,m=>["he sells ","sea","shells","shore"][m])

https://tio.run/##TYu9DoIwFIV3nuKmi5DUAnUxIWV3xg2JVKyAKRR6kcSnr8XBuJyfnO885Sqxsf207NejQ@GuIq@LJOVwe8PSKUgPNDh754CJtL5zCqfdAPiyigWFgf7hF8@b8Z8ff9C2HoCzmlk1admoML7c45YOIi@JP6DSGoFQgkpu2m39G4xVpCqHKnIzCMAwyoLGjGi0Ytq04Rxl7gM

8. Add I'm sure to the list (144: -4):

_=>`S012 by the 13, The 2 s0are 12,4. So if s012 on the 13, Then4 s013 2.`.replace(/\d/g,m=>["he sells ","sea","shells","shore"," I'm sure"][m])

https://tio.run/##TYqxDoIwFEV3vuKFRUhqgcJgQsrujBsSqVgBUyj0IYlfX4uTy7333JyX2AS2ZpjX43ayyO2NF00ZJwzuH1h7CUlKvItrBhgL45iRjHqlhuHpHufp6d@bsv1NgdGGGjkr0coguj6ijoy8qHwnolQKwSc@SrFnv/NvaCNdw/kwAr7drquxDu0CHDAIc6/VE2olqdJdsIS5/QI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment