DEV Community

Discussion on: Daily Challenge #42 - Caesar Cipher

Collapse
 
ynndvn profile image
La blatte

Here we go:

f=(s)=>[...Array(26)].map((_,i)=>26-i).map(n=>`key: ${26-n}\ncipher: ${s}\nplain: ${[...s].map(l=>(a=l.charCodeAt(0),String.fromCharCode(a<123&&a>96?97+((a-97+n)%26):a))).join('')}`).join`\n---\n`

Without any proper dictionary, I guess I'll stick to the "bruteforce" method! Here's how it works:

  • First, build an Array containing numbers from 26 to 1 ([...Array(26)].map((_,i)=>26-i))
  • Print the current index (26-n) and the cipher (s)
  • Build the associated original text: For each character, if its charCode is between 97 and 122 (hence, if it is a valid lowercase character), add the current index to it, else just return it.
  • Finally, print the complete result
f('dwwdfn iurp wkh zrrgv dw gdzq');

"key: 0
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: dwwdfn iurp wkh zrrgv dw gdzq
---
key: 1
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: cvvcem htqo vjg yqqfu cv fcyp
---
key: 2
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: buubdl gspn uif xppet bu ebxo
---
key: 3
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: attack from the woods at dawn
---
key: 4
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: zsszbj eqnl sgd vnncr zs czvm
---
key: 5
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: yrryai dpmk rfc ummbq yr byul
---
key: 6
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: xqqxzh colj qeb tllap xq axtk
---
key: 7
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: wppwyg bnki pda skkzo wp zwsj
---
key: 8
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: voovxf amjh ocz rjjyn vo yvri
---
key: 9
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: unnuwe zlig nby qiixm un xuqh
---
key: 10
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: tmmtvd ykhf max phhwl tm wtpg
---
key: 11
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: sllsuc xjge lzw oggvk sl vsof
---
key: 12
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: rkkrtb wifd kyv nffuj rk urne
---
key: 13
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: qjjqsa vhec jxu meeti qj tqmd
---
key: 14
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: piiprz ugdb iwt lddsh pi splc
---
key: 15
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: ohhoqy tfca hvs kccrg oh rokb
---
key: 16
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: nggnpx sebz gur jbbqf ng qnja
---
key: 17
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: mffmow rday ftq iaape mf pmiz
---
key: 18
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: leelnv qczx esp hzzod le olhy
---
key: 19
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: kddkmu pbyw dro gyync kd nkgx
---
key: 20
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: jccjlt oaxv cqn fxxmb jc mjfw
---
key: 21
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: ibbiks nzwu bpm ewwla ib liev
---
key: 22
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: haahjr myvt aol dvvkz ha khdu
---
key: 23
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: gzzgiq lxus znk cuujy gz jgct
---
key: 24
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: fyyfhp kwtr ymj bttix fy ifbs
---
key: 25
cipher: dwwdfn iurp wkh zrrgv dw gdzq
plain: exxego jvsq xli asshw ex hear"