Update Atbash.js

This commit is contained in:
Bharat Arya
2020-10-03 15:23:24 +05:30
committed by GitHub
parent 4a8443dda8
commit 5023cdd7cc

View File

@ -5,20 +5,17 @@ so that the first letter becomes the last letter,
the second letter becomes the second to last letter, and so on. the second letter becomes the second to last letter, and so on.
*/ */
/** /**
* Decrypt a Atbash cipher * Decrypt a Atbash cipher
* @param {String} str - string to be decrypted/encrypt * @param {String} str - string to be decrypted/encrypt
* @return {String} decrypted/encrypted string * @return {String} decrypted/encrypted string
*/ */
function Atbash(message) {
const Atbash = (message) => {
let decodedString = '' let decodedString = ''
for(let i = 0; i < message.length; i++) {
for (let i = 0; i < message.length; i++) { if(/[^a-zA-Z]/.test(message[i])) {
if (/[^a-zA-Z]/.test(message[i])) {
decodedString += message[i] decodedString += message[i]
} else if (message[i] === message[i].toUpperCase()) { } else if(message[i] === message[i].toUpperCase()) {
decodedString += String.fromCharCode(90 + 65 - message.charCodeAt(i)) decodedString += String.fromCharCode(90 + 65 - message.charCodeAt(i))
} else { } else {
decodedString += String.fromCharCode(122 + 97 - message.charCodeAt(i)) decodedString += String.fromCharCode(122 + 97 - message.charCodeAt(i))
@ -26,9 +23,7 @@ const Atbash = (message) => {
} }
return decodedString return decodedString
} }
// Atbash Example // Atbash Example
const encryptedString = 'HELLO WORLD' const encryptedString = 'HELLO WORLD'
const decryptedString = Atbash(encryptedString) const decryptedString = Atbash(encryptedString)
console.log(decryptedString) // SVOOL DLIOW console.log(decryptedString) // SVOOL DLIOW