Skip to content

Instantly share code, notes, and snippets.

@NeniEmSu
Last active August 30, 2020 12:50
Show Gist options
  • Save NeniEmSu/08e51bc7cada46fd70bc967dfef1ec62 to your computer and use it in GitHub Desktop.
Save NeniEmSu/08e51bc7cada46fd70bc967dfef1ec62 to your computer and use it in GitHub Desktop.
Write a function which takes a ROT13 encoded string as input and returns a decoded string. All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.
function rot13(str) {
const splitStr = str.split('')
let finalArr = []
for(let i = 0; i < splitStr.length; i++){
for(let j = 0; j < splitStr[i].split('').length; j++){
const currentValueCode
= splitStr[i]
.split('')[j]
.charCodeAt()
if(currentValueCode < 65 || currentValueCode > 90 ) finalArr.push(currentValueCode)
else if(currentValueCode < 78) finalArr.push(currentValueCode + 13)
else finalArr.push(currentValueCode - 13)
}
}
return String.fromCharCode(...finalArr)
}
console.log(rot13("SERR CVMMN!")); \\ FREE PIZZA!
console.log(rot13("SERR PBQR PNZC")); \\ FREE CODE CAMP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment