DEV Community

Discussion on: 1 Line Caesar Cipher

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Fixed version:

const caesarCipher = (str, shift=13) => str.replace(
  /[A-Za-z]/g, char => String.fromCharCode(
    65+((char=char.charCodeAt())&32)+((char&~32)-65+shift)%26
  )
)

caesarCipher('JON')      // WBA
caesarCipher('jon')      // wba
caesarCipher('Jon')      // Wba
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tebohom profile image
mosemet

Thanks Jon. I love this.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

Also, to get JS syntax highlighting in your post - add js after the opening 3 backticks 👍