Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

71%
+3 −0
Challenges Caesar shift cipher

JavaScript (Node.js), 67 60 bytes o=>r=>Buffer(o).map(c=>c%32<26&c>64?(c%32+r)%26+c-c%32:c)+'' Try it online! o=>r=> // Define a function taking o an...

posted 3y ago by emanresu A‭  ·  edited 3y ago by emanresu A‭

Answer
#2: Post edited by user avatar emanresu A‭ · 2021-08-07T00:30:42Z (over 3 years ago)
  • # [JavaScript (Node.js)], 67 bytes
  • <!-- language-all: lang-javascript -->
  • o=>r=>(b=Buffer)(b(o).map(c=>c%32<26&c>64?(c%32+r)%26+c-c%32:c))+''
  • [Try it online!][TIO-krnf2y5z]
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-krnf2y5z]: https://tio.run/##FcfBDoIwDADQX/GCa7NA4jA7GDsST575g1GG0VRKBurnz/Bu7xW/ceX8XLZ61jGVnopSyBRgoNtnmlJGGECxeccFmAJXrbs6f@Tgzx3ssxkr5y3Xey6MaI0prPOqkhrRB/Rg7klEDz/NMhqEU4tY/g "JavaScript (Node.js) – Try It Online"
  • ```
  • o=>r=>( // Define a function taking o and r
  • b = Buffer // Assign b to Buffer
  • )( // And call on...
  • b(o).map(c=> // a Buffer of the charcodes of o, mapped to...
  • c%32<26&c>64 // If the character is alphabetical - charcode%32 is less than 26, charcode is >64
  • ? // Then
  • (c%32+r)%26+c-c%32 // Rot-n the character - Mod 32, add r, mod 26, add correct number depending on whether it's uppercase of lowercase.
  • :c) // Else return the original string
  • ) + '' // Coerce to string
  • ```
  • # [JavaScript (Node.js)], <s>67</s> 60 bytes
  • <!-- language-all: lang-javascript -->
  • o=>r=>Buffer(o).map(c=>c%32<26&c>64?(c%32+r)%26+c-c%32:c)+''
  • [Try it online!][TIO-ks11i9h2]
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-ks11i9h2]: https://tio.run/##FcfBDoIwDADQX/GCa7NAwiA7GDcTT5z5g6UMo6mUFJXPn@Hd3iv90kb6XD/1IlMuYygSooZ4/85zVhBs3mkFCpGqzl2dP1P0/Q2OWcXKeUv1kQuhNaaQLJtwblgeMIIZMrOcdlGeDELbIZY/ "JavaScript (Node.js) – Try It Online"
  • ```
  • o=>r=> // Define a function taking o and r, and returning...
  • Buffer(o).map(c=> // a Buffer of the charcodes of o, mapped to...
  • c%32<26&c>64 // If the character is alphabetical - charcode%32 is less than 26, charcode is >64
  • ? // Then
  • (c%32+r)%26+c-c%32 // Rot-n the character - Mod 32, add r, mod 26, add correct number depending on whether it's uppercase of lowercase.
  • :c) // Else return the original string
  • ) + '' // Coerce to string
  • ```
  • -7 thanks to Shaggy.
#1: Initial revision by user avatar emanresu A‭ · 2021-07-28T11:45:46Z (over 3 years ago)
# [JavaScript (Node.js)], 67 bytes

<!-- language-all: lang-javascript -->

    o=>r=>(b=Buffer)(b(o).map(c=>c%32<26&c>64?(c%32+r)%26+c-c%32:c))+''

[Try it online!][TIO-krnf2y5z]

[JavaScript (Node.js)]: https://nodejs.org
[TIO-krnf2y5z]: https://tio.run/##FcfBDoIwDADQX/GCa7NA4jA7GDsST575g1GG0VRKBurnz/Bu7xW/ceX8XLZ61jGVnopSyBRgoNtnmlJGGECxeccFmAJXrbs6f@Tgzx3ssxkr5y3Xey6MaI0prPOqkhrRB/Rg7klEDz/NMhqEU4tY/g "JavaScript (Node.js) – Try It Online"

```
o=>r=>(                // Define a function taking o and r
  b = Buffer           // Assign b to Buffer
)(                     // And call on...
  b(o).map(c=>         // a Buffer of the charcodes of o, mapped to...
    c%32<26&c>64       // If the character is alphabetical - charcode%32 is less than 26, charcode is >64
      ?                // Then
    (c%32+r)%26+c-c%32 // Rot-n the character - Mod 32, add r, mod 26, add correct number depending on whether it's uppercase of lowercase.
    :c)                // Else return the original string
 ) + ''                // Coerce to string
```