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开发者_Go百科()
{
//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>
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;
}
精彩评论