Karaca's encryption is an algorithm
commonly used for programming excercises.
It's used to teach the fundamentals of
working with strings, arrays, dicts, and
anything you can make the encryption algorithm with.
As every encryption algorithm, it lets you
encrypt text following some rules, which the
person decoding the text has to Understand in
order to decrypt it.
Karaca'salgorithm follows a series of steps:
A = 0 | E = 1 | I = 2 | O = 3 | U = 4
The encryption() function approach is really simple,
following the steps of the already known process.
First, we create a vowel array to know what vowel goes for each number.
Then we reverse the word by using JS built-in functions for turning
a string into an array (split("")
) and reversing an array (reverse()
).
On the second step we convert the vowels, helped by an extra
variable where we're going to store what goes into the converted word.
We for
our way through the reversed array and evaluate whether
the character that we're reading is a vowel or not.
If it is, we're going to find it in our vowels
array
and we'll use the index to fill the character.
Lastly, we just add an "aca"
string to the end of it
and call it a day.
The process for decryption is really easy when
you get the encryption function down.
We're going to keep the same vowels
array, and we'll
start by substracting the "aca"
substring to the end
of our word with the slice function that keeps all
the characters between 0 and -3, and gets rid of
the excess.
Secondly, we reverse the word using the same method
we used on the encryption function.
Lastly we convert the vowels by going through all the
characters on our array and checking with the
/\d/.test(word[i])
regex if it's a number.
If it is, we just use it as the index for the
vowel to use.