: Hello Again Everyone
:
: This is what I have so far (with help from zibadian). As I have mentioned before, I am trying to write something simple, that takes input from a user and using a simple substitution cipher encrypts the users input.
:
: Can anyone see (because I have now lost my marbles) the problem, when I enter a string I cannot get an encrypted output. Im hoping its something fairly simple but knowing me Ive missed a whole chunk of something. I can get output but it keeps showing 'undefined'.
:
: Can anyone help?
:
:
: function doEncrypt()
: {
:
: var givenString,encodedString;
: document.encipher.outputString.value = '';
: givenString = document.encipher.inputString.value;
: givenString = givenString.toLowerCase();// note that toLowerCase leaves non-alpha characters unchanged
: encodedString = subEncrypt(givenString);
: document.encipher.outputString.value = encodedString;
: document.encipher.inputString.focus();// reset focus
:
: }
:
:
: function subEncrypt(aString)
: {
: var cipherArray =
: ['s','c','h','n','i','t','z','e','l','m','o','p','q','r','u',
: 'v','w','x','y','a','b','d','f','g','j','k']; // schnitzel is the key
:
:
: var characterArray =
: ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
: 'p','q','r','s','t','u','v','w','x','y','z'];
:
:
: var encryptedString = '';
: var charIndex = 0;
:
: for (var i = 0; i < aString.length; i++)
: {
: for (var j = 0; j < characterArray.length; j++)
: {
: if (aString.length[i] == characterArray[j])
: {
: charIndex = j;
: }
: }
: };
: encryptedString = encryptedString + cipherArray[charIndex]
: }
:
:
: Any help appreciated.
:
:
All you need to add is the return statement to the subEncrypt() function. Currently it encode the string, but doesn't return the resulting value. The code for that is quite simple:
return encryptedString;
Also there are several mistakes in the encryption routine. First, the statement
encryptedString = encryptedString + cipherArray[charIndex]
must be inside the outer loop, otherwise it will only add the last character and not the rest.
Secondly, the line
if (aString.length[i] == characterArray[j])
is invalid, since the length property is an integer and not an array. To get a single character from a string, you need to say:
if (aString[i] == characterArray[j])
After you made thse changes and additions it should work.