Skip to content

Commit 56a521c

Browse files
authored
Create CaesarCipher.js
1 parent d3d077e commit 56a521c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

9. Caesar Cipher/CaesarCipher.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const allShifts = [...Array(26).keys()]
2+
const cipher = (str, keys=allShifts) => {
3+
const chars = str.split("")
4+
const res = keys.map( key => {
5+
return chars.map( c => {
6+
const char = c.charCodeAt(0)
7+
if(char > 64 && char < 91)
8+
{
9+
return String.fromCharCode(65 + (char + key) % 65 % 26)
10+
}
11+
else if(char > 96 && char < 123)
12+
{
13+
return String.fromCharCode(97 + (char + key) % 97 % 26)
14+
}
15+
else
16+
{
17+
return String.fromCharCode(char)
18+
}
19+
})
20+
})
21+
22+
return res.map( (r, i) => { return { key: keys[i], result: r.join("") } } )
23+
}

0 commit comments

Comments
 (0)