“JavaScript Caesar Cipher” Ответ

JavaScript Caesar Cipher

const caesarEncoded = (s, n) => {
	let alphabet = 'abcdefghijklmnopqrstuvwxyz'
	let lc = alphabet.replace(/\s/g, '').toLowerCase().split('')
	let uc = alphabet.replace(/\s/g, '').toUpperCase().split('')

	return Array.from(s)
		.map((v) => {
			if (lc.indexOf(v.toLowerCase()) === -1 || uc.indexOf(v.toUpperCase()) === -1) {
				return v
			}

			const lcEncryptIndex = (lc.indexOf(v.toLowerCase()) + n) % alphabet.length
			const lcEncryptedChar = lc[lcEncryptIndex]

			const ucEncryptIndex = (uc.indexOf(v.toUpperCase()) + n) % alphabet.length
			const ucEncryptedChar = uc[ucEncryptIndex]

			return lc.indexOf(v) !== -1 ? lcEncryptedChar : ucEncryptedChar
		})
		.join('')
}

const caesarDecoded = (s, n) => {
	let alphabet = 'abcdefghijklmnopqrstuvwxyz'
	let lc = alphabet.replace(/\s/g, '').toLowerCase().split('')
	let uc = alphabet.replace(/\s/g, '').toUpperCase().split('')

	return Array.from(s)
		.map((v) => {
			if (lc.indexOf(v.toLowerCase()) === -1 || uc.indexOf(v.toUpperCase()) === -1) {
				return v
			}

			let lcEncryptIndex = (lc.indexOf(v.toLowerCase()) - n) % alphabet.length
			lcEncryptIndex = lcEncryptIndex < 0 ? lcEncryptIndex + alphabet.length : lcEncryptIndex
			const lcEncryptedChar = lc[lcEncryptIndex]

			let ucEncryptIndex = (uc.indexOf(v.toUpperCase()) - n) % alphabet.length
			ucEncryptIndex = ucEncryptIndex < 0 ? ucEncryptIndex + alphabet.length : ucEncryptIndex
			const ucEncryptedChar = uc[ucEncryptIndex]

			return lc.indexOf(v) !== -1 ? lcEncryptedChar : ucEncryptedChar
		})
		.join('')
}

console.log(caesarEncoded('XVCJ9', 5))
console.log(caesarDecoded('CAHO9', 5))
Restu Wahyu Saputra

JavaScript Caesar Cipher

const alphabet = [
	'A',
	'B',
	'C',
	'D',
	'E',
	'F',
	'G',
	'H',
	'I',
	'J',
	'K',
	'L',
	'M',
	'N',
	'O',
	'P',
	'Q',
	'R',
	'S',
	'T',
	'U',
	'V',
	'W',
	'X',
	'Y',
	'Z'
]
const MAX_ROT = alphabet.length

/**
 * Encrypt text  using spanish Cesar encryption
 * @memberof crypt/cesar
 * @param {string} text - A string to be encrypted
 * @param {number} rot - Scroll numer, can be from 0 to 26
 * @returns {string} - Encrypted text
 */
const encrypt = (text, rot = 3) => {
	if (rot > MAX_ROT || rot < 0) {
		throw Error('rot can be only beetween 0 to 26')
	}

	text = Array.from(text)

	const encryptedText = text.map((char) => {
		const isLower = char.toLowerCase()
		const idx = alphabet.indexOf(char.toUpperCase())

		if (idx === -1) {
			return char
		}

		const encryptedIdx = (idx + rot) % MAX_ROT
		const encryptedChar = alphabet[encryptedIdx]

		return isLower ? encryptedChar.toLowerCase() : encryptedChar
	})

	return encryptedText.join('')
}

/**
 * Decrypt text using spanish Cesar encryption
 * @memberof crypt/cesar
 * @param {string} text - A string to be decrypted
 * @param {number} rot - Scroll numer, can be from 0 to 26
 * @returns {string} - Decrypted text
 */
const decrypt = (text, rot = 3) => {
	if (rot > MAX_ROT || rot < 0) {
		throw Error('rot can be only beetween 0 to 26')
	}

	text = Array.from(text)

	const decryptedText = text.map((char) => {
		const isLower = char.toLowerCase()
		const idx = alphabet.indexOf(char.toUpperCase())

		if (idx === -1) {
			return char
		}

		let decryptedIdx = (idx - rot) % MAX_ROT

		decryptedIdx = decryptedIdx < 0 ? decryptedIdx + MAX_ROT : decryptedIdx

		const decryptedChar = alphabet[decryptedIdx]

		return isLower ? decryptedChar.toLowerCase() : decryptedChar
	})

	return decryptedText.join('')
}

console.log(encrypt('abcxyz', 5))
console.log(decrypt('fghcde', 5))
Restu Wahyu Saputra

Ответы похожие на “JavaScript Caesar Cipher”

Вопросы похожие на “JavaScript Caesar Cipher”

Больше похожих ответов на “JavaScript Caesar Cipher” по JavaScript

Смотреть популярные ответы по языку

Смотреть другие языки программирования