mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
Update Atbash.js
This commit is contained in:
@ -1,8 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
The Atbash cipher is a particular type of monoalphabetic cipher
|
The Atbash cipher is a particular type of monoalphabetic cipher
|
||||||
formed by taking the alphabet and mapping it to its reverse,
|
formed by taking the alphabet and mapping it to its reverse,
|
||||||
so that the first letter becomes the last letter,
|
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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -10,18 +10,18 @@ the second letter becomes the second to last letter, and so on.
|
|||||||
* @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) {
|
function 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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return decodedString
|
return decodedString
|
||||||
}
|
}
|
||||||
// Atbash Example
|
// Atbash Example
|
||||||
const encryptedString = 'HELLO WORLD'
|
const encryptedString = 'HELLO WORLD'
|
||||||
|
Reference in New Issue
Block a user