96 - Rot13

Q.

Description:
ROT13 is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher.

Create a function that takes a string and returns the string ciphered with Rot13. If there are numbers or special characters included in the string, they should be returned as they are. Only letters from the latin/english alphabet should be shifted, like in the original Rot13 "implementation".

A)

function rot13(message){
  //your code here
  let res = message.split('')
  return res.map(el => {
    if (el.charCodeAt() < 65) return el.charCodeAt()
    if (el.charCodeAt() - 13 < 65) return el.charCodeAt()+13
    if (el.charCodeAt() === el.toUpperCase().charCodeAt()) return el.charCodeAt() -13
    if (el.charCodeAt() - 13 < 97) return el.charCodeAt()+13
    return el.charCodeAt()-13
    }).map(el => String.fromCharCode(el)).join('')
}

좋은 웹페이지 즐겨찾기