Answers for "caesar cipher javascript"

0

caesar cipher javascript

function rot13(str) {
var charcode = [];
var words = [];

for(let i = 0; i < str.length; i++){
  
  var asc = str.charCodeAt(i)
  
  charcode.push(asc)
}
var converted = charcode.map(function(a){
  return a-13
})
console.log(converted)
converted.forEach((letter)=>{
 if(letter >= 65){
  var final =String.fromCharCode(letter)
  words.push(final)
 }
 else if(letter>=52){
   final = String.fromCharCode(letter+26)
   words.push(final)
 }
 else {
   final = String.fromCharCode(letter+13)
   words.push(final)
 }
 
})
return words.join("")
}

console.log(rot13("SERR YBIR?"));
Posted by: Guest on July-01-2021
0

Caesars Cipher javascript

var myCipher = [];
      var myArray = []; 
      
      for (i=0; i < str.length; i++) {
        // convert character - or don't if it's a punctuation mark.  Ignore spaces.
        if (str.charCodeAt(i) > 64 && str.charCodeAt(i) < 78) {
             myArray.push(String.fromCharCode(str.charCodeAt(i) + 13));
         } else if (str.charCodeAt(i) > 77 && str.charCodeAt(i) < 91) { 
             myArray.push(String.fromCharCode(77 - (90 - str.charCodeAt(i))));
         } else if (str.charCodeAt(i) > 32 && str.charCodeAt(i) < 65) {
             myArray.push(str.charAt(i));
         }
       
        // push word onto array when encountering a space or reaching the end of the string
        
        if (str.charCodeAt(i) == 32) {
          myCipher.push(myArray.join(''));
          myArray.length = 0;      
        }
        
        if (i == (str.length - 1)) {
           myCipher.push(myArray.join(''));
        }

      }

      return myCipher.join(" ");
      
    }
Posted by: Guest on July-12-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language