1

I have the following program but when i try to encrypt word JAVASCRIPT it seems like function 'rotateToPosition' within function 'encrypt' doesnt work. The answer i get is Xjavascript. It displays symbol character X at the beggining as i want it and it changes letters from plainArray to cipherArray but it doesent shift them. The answer i'm looking for is Xzqlqishyfj. Does anyone know i do wrong? Thanks.

<HTML>
<HEAD>
<TITLE> 
Alberti's Disks
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
 // array of upper case letters
 var plainArray = ['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'];
 // array of lower case letters
 var cipherArray = ['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'];
//The function shift all elements to the rignt
function right(letterArray)
{
 var ShiftToRightArray = new Array (26); //creates new array
 for (var count = 0; count < 25; count++) // checks every argument in the array
 {
 ShiftToRightArray[count + 1] = letterArray[count]; //assigns one argument from letterArray to index from new array increased by one
 }
 ShiftToRightArray[0] = letterArray[25]; //assigns last argument from letterArray to first index in new array
 return ShiftToRightArray; //returns value of new array
}
//The function simulates the rotation of a cipher alphabet
function rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
{
 var rotateArray = new Array (26);
 rotateArray = cipherAlphabet;
 while (rotateArray[0] != indexCharacter)
 {
 rotateArray = right(rotateArray);
 }
 return rotateArray;
}
//Function to encrypt given word
function encrypt(plainText, signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
{
 // rotate array to signal character position
 rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
 //will hold the results of the encrpytion until it can be appended to encryptedString
 var encryptedString = signalCharacter;
 for (var count = 0; count < plainText.length; count++)
 {
 var singleLetter = plainText.charAt(count);
 var i = plainAlphabet.indexOf(singleLetter);
 encryptedString = encryptedString + cipherAlphabet[i];
 }
 return encryptedString;
}
function testTask02()
{
 window.alert(right(cipherArray));
}
function testTask03()
{
 window.alert(rotateToPosition('A', 'g', plainArray, cipherArray));
}
function testTask04()
{
//encrypts word JAVASCRIPT using X as signal character and n as index character
 window.alert(encrypt('JAVASCRIPT', 'X', 'n', plainArray, cipherArray));
}
</SCRIPT>
</HEAD>
<BODY>
 <FORM NAME = "Form">
 <STRONG>SIMULATING ALBERTI'S DISKS<BR></STRONG>
 <P>
 <INPUT TYPE = "button" NAME = "task02Button" VALUE ="Test Task 2"
 ONCLICK = " testTask02() ;"> 
 </P>
 <P>
 <INPUT TYPE = "button" NAME = "task03Button" VALUE ="Test Task 3"
 ONCLICK = " testTask03() ;"> 
 </P>
 <P>
 <INPUT TYPE = "button" NAME = "task04Button" VALUE ="Test Task 4"
 ONCLICK = " testTask04() ;"> 
 </P>
 </FORM>
</BODY>
</HTML>
asked May 22, 2011 at 10:01

1 Answer 1

2

You do not use the return value of your call to rotateToPosition. I suspect it should read something like

cipherAlphabet = rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet);

Addon: The function rotateToPosition does not take into account the signalCharacter. It should be corrected to do so:

function rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
{
 var rotateArray = new Array (26);
 rotateArray = cipherAlphabet;
 var i = plainAlphabet.indexOf(signalCharacter);
 while (rotateArray[i] != indexCharacter)
 {
 rotateArray = right(rotateArray);
 }
 return rotateArray;
}
answered May 22, 2011 at 10:14
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. it encrypts the message but the answer i get now is Xwninfpevcg instead of Xzqlqishyfj. It seems like cipherAlphabet is shifted 3 places off to the left. Do you know why? It looks to me like it should be working ok now ;(
@Mark I didn't check in detail but it looks fine to me. The second and fourth chars of your text are as and those are mapped to n (your index character).
@Mark: to me it looks ok too. An index character of 'n' means 13 shifts, so 'J' should map to 'w', 'A' to 'n', 'V' to 'i', etc. Maybe the cipherArray you've seen/used to get a result of Xzqlqishyfj wasn't plain from a..z, but starting d..za..c, or it was plain a..z but an an index character of 'p' was used?
An index character is 'n' but there is also a signal character 'X' used which means that letter 'X' from plainArray should be alligned with letter 'n' from cipherArray and all the other characters encrypted acording to that alignment. Function rotateToPosition works fine alone but seems to work different when used in function encrypt. So if we allign 'X' with 'n' then 'J' should be 'z', 'A' should be 'q' etc. Any ideas?
I've just realised that rotateToPosition function is not right. I think it only works with signal character A. I will try to change it now.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.