139b. Encrypt and decrypt text using affine cipher method.
Invoke function using 4 arguments.
1st arg (string) ... Input pain/cipher text. Its can contain only English alphabet [a..z], no punctuation.
2nd arg (number) ... The multiplication number. Only [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25] are valid.
3rd arg (number) ... The key number. Range [0..25].
4th arg (number) ... Direction of encrypt/decrypt. 1 for encryption and 0 for decryption.
By studying @wrayal's Vigenère Cipher. I decide to implement the popular well-known Caesar cipher again.
But then I realized that Caesar cipher is just one special case from Vigenère cipher. That's mean I done nothing new! So I rewrite this function in Affine cipher. Which still can be use as the Caesar cipher by setting the 2nd arg = 1, e.g. function("texthere", 1, 4, 1)
.
If you just "use" a variable (i.e. assign a value to it) without declaring it first, it implicitly is global (i.e. it belongs to the outer most scope, which is in this case the window scope).
That means, after calling your function, we can get the value of "i" in the outer scope. Or even worse, if we would have a variable named "i" in outer scope, you would overwrite it.
Declaring them with the
var
keyword (inside you function) would a valid method. But you could save yourself two bytes, just add them as parameters to your function head. The caller doesn't need to supply them, but you can then use them inside your function just as any other variable.