If you intend to process more than one letter, it is best to prepare an encoder / decoder (function or array) in advance. Then your work becomes as simple as:
output = input.map(val => encoder[val] || val);
Part || val || val is for values not processed by the encoder.
Working example
In this example, we use split("") to convert the string to an array and join("") to do the opposite.
PrepareCodec: var Latin = "abcdefghijklmnoprstuvyzABCDEFGHIJKLMNOPRSTUVYZ"; var Cyrillic = ""; var encoder = {}, decoder = {}; for (let i in Latin) encoder[Latin[i]] = Cyrillic[i]; for (let i in Cyrillic) decoder[Cyrillic[i]] = Latin[i]; EncryptionTest: var src = prompt("Enter text to encrypt:", "Hello, world!"); var enc = src.split("").map(val => encoder[val] || val).join(""); DecryptionTest: enc = prompt("Enter text to decrypt:", enc); var dec = enc.split("").map(val => decoder[val] || val).join(""); FinalResult: prompt("Decrypted text:", dec);
source share