Multiple exercises were added to the TypeScript track in this commit.
This commit is contained in:
21
typescript/atbash-cipher/atbash-cipher.ts
Normal file
21
typescript/atbash-cipher/atbash-cipher.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
const originalAlphabet = 'abcdefghijklmnopqrstuvwxyz'
|
||||
const cipherAlphabet = 'zyxwvutsrqponmlkjihgfedcba'
|
||||
|
||||
export function encode(plainText: string): string {
|
||||
return plainText
|
||||
.toLowerCase()
|
||||
.split('')
|
||||
.map(c => c.match(/\d/) ? c : cipherAlphabet[originalAlphabet.indexOf(c)])
|
||||
.filter(Boolean)
|
||||
.map((c, i) => (i + 1) % 5 === 0 ? `${c} ` : c)
|
||||
.join("")
|
||||
.trim()
|
||||
}
|
||||
|
||||
export function decode(cipherText: string): string {
|
||||
return cipherText
|
||||
.split('')
|
||||
.map(c => c.match(/\d/) ? c : originalAlphabet[cipherAlphabet.indexOf(c)])
|
||||
.filter(Boolean)
|
||||
.join("")
|
||||
}
|
||||
Reference in New Issue
Block a user