我有以下程序但是当我尝试加密单词JAVASCRIPT时,似乎函数'encrypt'中的函数'rotateToPosition'不起作用。我得到的答案是Xjavascript。它在开始时显示符号字符X,因为我想要它,它会将plainArray中的字母更改为cipherArray,但它会转移它们。我正在寻找的答案是Xzqlqishyfj。有谁知道我做错了吗? 感谢。
<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>
答案 0 :(得分:2)
您不使用致电rotateToPosition
的返回值。我怀疑它应该读起来像
cipherAlphabet = rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet);
插件:[{1}}功能未考虑rotateToPosition
。应该纠正这样做:
signalCharacter