Write a Javascript program that implements the Caesar Cipher algorithm. The program must take the message to encrypt and the number of shifting positions, N, as a command line argument, as illustrated in the figure below. Make sure that your program does not crash or give unexpected results when given lower case letters or any other character.
This is what I have so far:
function encrypt(str, N) {
var result =
var Plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
for (var i = 0; i < str.length; i ++) {
index = Plain.indexOf(str[i]);
result = Plain[index];
}
return result;
}
CODE
//decipher the string let ceaserCipher = () => { let str = process.argv[2]; let N = parseInt(process.argv[3]); let decipher = ''; //decipher each letter for(let i = 0; i < str.length; i++){ //if letter is uppercase then add uppercase letters if(str[i] >= 'A' && str[i] <= 'Z'){ decipher += String.fromCharCode((str.charCodeAt(i) + N - 65) % 26 + 65); }else if(str[i] >= 'a' && str[i] <= 'z'){ decipher += String.fromCharCode((str.charCodeAt(i) + N - 97) % 26 + 97); } } return decipher; }
Get Answers For Free
Most questions answered within 1 hours.