CaesarCipher string manipulation

function CaesarCipher(str,num) { 

  if(num < 0) {
    return CaesarCipher(str, num + 26);
  }

  let output = "";
  for(let i=0; i<str.length; i++) {
    let c = str[i];
    if(c.match(/[a-z]/i)) {
      let code = str.charCodeAt(i);

      if(code >= 65 && code <= 90) {
        c = String.fromCharCode(((code - 65 + num) % 26) + 65);
      } else if(code >= 97 && code <= 122)  {
        c = String.fromCharCode(((code-97 + num) % 26) + 97);
      }

    }

    output += c;
    
  }
  // code goes here  
  return output; 

}
   
// keep this function call here 
console.log(CaesarCipher("A", 2)); // output is c

Leave a Reply

Your email address will not be published. Required fields are marked *