MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / ROT13

Function ROT13

Ciphers/ROT13.js:8–22  ·  view source on GitHub ↗

* @function ROT13 * @description - ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the alphabet. ROT13 is a special case of the Caesar cipher which was developed in ancient Rome. Because there

(str)

Source from the content-addressed store, hash-verified

6 * @return {String} decrypted string
7 */
8function ROT13(str) {
9 if (typeof str !== 'string') {
10 throw new TypeError('Argument should be string')
11 }
12
13 return str.replace(/[a-z]/gi, (char) => {
14 const charCode = char.charCodeAt()
15
16 if (/[n-z]/i.test(char)) {
17 return String.fromCharCode(charCode - 13)
18 }
19
20 return String.fromCharCode(charCode + 13)
21 })
22}
23
24export default ROT13

Callers 1

ROT13.test.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected