Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function rot13(encodedStr) {
- var codeArr = encodedStr.split(""); // String to Array
- var decodedArr = []; // Your Result goes here
- // Only change code below this line
- var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- for (var c = 0; c < codeArr.length; c++) //Count the number of words in codeArr
- {
- var newWord = "";
- var newLetter = "";
- for (var i = 0; i < codeArr[c].length; i++) //Count the number of letters in codeArr[currentWord]
- {
- var letterCode = codeArr[c][i].charCodeAt(0); //Convert to ASCII Code
- if ((letterCode >= 65) && (letterCode <= 90)) //If the letter is A-Z
- {
- for (var a = 0; codeArr[c][i] !== alphabet[a]; a++) //Go through alphabet checking for current letter
- {
- var newIndex = Math.abs(13 - a);
- newLetter = alphabet[alphabet.length - newIndex];
- }
- }
- else
- {
- newLetter = codeArr[c][i];
- }
- newWord += newLetter; //Add the newLetter to the newWord
- newLetter = ""; //Clear the newLetter variable
- }
- decodedArr.push(newWord); //Add the newWord to the decodedArr
- }
- // Only change code above this line
- return decodedArr.join(""); // Array to String
- }
- // Change the inputs below to test
- rot13("SERR PBQR PNZC");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement