Info: Those interested in ciphers should consider competing in the National Cipher Challenge that starts this month.
Supposedly, Caesar used to 'encrypt' confidential messages by shifting each letter by some number of places. For instance, he might write A as B, B as C, C as D, ... and, wrapping around alphabetically, Z as A. The message recipient would decrypt the message by shifting letters in the opposite direction. This is a very basic type of substitution cipher.
Unencrypted text is generally called plaintext, and encrypted text ciphertext. The secret (in this case, the number of letters to shift by) is called the key. Plaintext is usually written in lowercase and ciphertext in uppercase.
Encrypting hello with a key of 2 yields JGNNQ. Encrypting zero with a key of 3 yields CHUR.
In a file called caesar.js in a folder called caesar, write a program that enables you to encrypt messages using Caesar's cipher as follows:
the key should be provided as a command line argument, eg bun caesar.js 5 for a key of 5
if an integer isn't provided, the program should show the error message Provide a key (integer) as an argument and exit
they key can be any integer, including integers above 26 and including negative integers
on running, the program should ask the user for the plaintext with the message plaintext:
any character not in the range a-z should be unchanged in the ciphertext
the program will then print ciphertext: with the correct ciphertext shown
the ciphertext should be shown in ALL CAPS
the program should then exit
Here is an example session with the program:
❯ bun caesar.js hi
Provide a key (integer) as an argument
❯ bun caesar.js 3.4
Provide a key (integer) as an argument
❯ bun caesar.js 2
plaintext: hello
ciphertext: JGNNQ
❯ bun caesar.js 2049
plaintext: excellent
ciphertext: ZSXZGGZIO
❯ bun caesar.js -1
plaintext: excellent work! 123 :)
ciphertext: DWBDKKDMS VNQJ! 123 :)
you do not need arrays for this task, but you do need command line arguments
you may want to use 'hello'.toLowerCase() or 'hello'.toUpperCase()
'abcdefg'.indexOf('d') gives 3, ie 'd' is the 4th character in the string 'abcdefg'
'quick brown'[3] gives c, ie you can get the zero-indexed nth character of a string this way
18 % 4 gives 2, ie the remainder when dividing 18 by 4 is 2
you may find it useful to use let alpha = 'abcdefghijklmnopqrstuvwxyz'