Oct. 14 - Oct. 16
Test your code now! ... Like, ..., Seriously!
You simply must test your DecryptStep 1: Iterate over the secret messageThis page looks very similar to the encryption page. That's because you do almost the exactly same thing, only in reverse. The bold parts are what's different! In this function you will need to iterate over ("go through") the ciphertext property (using a for loop). data.ciphertext holds a String.Step 2: Get the ASCII code for each character in the messageYou can get the ASCII value for each character in the cipher text using thecharCodeAt() function like so data.ciphertext.charCodeAt(position). Note that since we are passing the character's position to a function, it is in parentheses (), not index brackets [].
Step 3: Apply the Substitutiondata.key holds the numerical value by which you have to shift your characters. This time you need to subtract this value as we are doing the exact opposite of what we did for encryption! Let's say your first letter is an m. In step 2 you got the ASCII value of 109. Now you just need to subtract data.key from that value. If data.key is 4, your new value should be 109-4=105. This is perfect. But what happens if data.key is 20? Then 109-20=89, but our alphabet begins with a at position 97. Well, do as Caesar did. Whenever your newly computed value drops below 97, just add 26 to jump to the end of the alphabet. 89+26=115. Perfect, that'll work!
Step 4: Find out what letter your new ASCII value represents.We have provided a function calledmakeCharacter for you that uses Javascript's powerful String object to resolve ASCII values to characters. You can call makeCharacter(115) and it will return "s".
Step 5: Build the Clear TextAs you go through the letters of the cipher text and substitute them with another one, append ("add") the substitute to thecleartext variable. You will notice that this variable gets returned at the end of the decrypt function, so this is where you need to store your decrypted message.
| ![]() |