1function rot13(str) {
2var charcode = [];
3var words = [];
4
5for(let i = 0; i < str.length; i++){
6
7 var asc = str.charCodeAt(i)
8
9 charcode.push(asc)
10}
11var converted = charcode.map(function(a){
12 return a-13
13})
14console.log(converted)
15converted.forEach((letter)=>{
16 if(letter >= 65){
17 var final =String.fromCharCode(letter)
18 words.push(final)
19 }
20 else if(letter>=52){
21 final = String.fromCharCode(letter+26)
22 words.push(final)
23 }
24 else {
25 final = String.fromCharCode(letter+13)
26 words.push(final)
27 }
28
29})
30return words.join("")
31}
32
33console.log(rot13("SERR YBIR?"));
1 var myCipher = [];
2 var myArray = [];
3
4 for (i=0; i < str.length; i++) {
5 // convert character - or don't if it's a punctuation mark. Ignore spaces.
6 if (str.charCodeAt(i) > 64 && str.charCodeAt(i) < 78) {
7 myArray.push(String.fromCharCode(str.charCodeAt(i) + 13));
8 } else if (str.charCodeAt(i) > 77 && str.charCodeAt(i) < 91) {
9 myArray.push(String.fromCharCode(77 - (90 - str.charCodeAt(i))));
10 } else if (str.charCodeAt(i) > 32 && str.charCodeAt(i) < 65) {
11 myArray.push(str.charAt(i));
12 }
13
14 // push word onto array when encountering a space or reaching the end of the string
15
16 if (str.charCodeAt(i) == 32) {
17 myCipher.push(myArray.join(''));
18 myArray.length = 0;
19 }
20
21 if (i == (str.length - 1)) {
22 myCipher.push(myArray.join(''));
23 }
24
25 }
26
27 return myCipher.join(" ");
28
29 }