嗨,我已经写了这个程序,但是当我按下按钮时,没有任何事情你知道为什么吗?
<HTML>
<HEAD>
<TITLE>
Alberti's Disks
</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
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'];
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'];
//this function shifts all elements of the array
function right(letterArray)
{
var rightShiftArray = new Array (26);
for (var count = 0; count < 25; count++)
{
rightShiftArray[count + 1] = letterArray[count];
}
rightShiftArray[0] = letterArray[25];
return rightShiftArray;
}
//this function rotates cipher alphabet
function rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
{
var rotateArray = new Array (26);
rotateArray = cipherAlphabet;
while (rotateArray[0] != indexCharacter)
{
rotateArray = right(rotateArray);
}
return rotateArray;
}
//this function encrypts given word using index character
function encrypt(plainText, signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet)
{
var encryptedString = signalCharacter;
//i is what will hold the results of the encrpytion until it can be appended to encryptedString
var i;
// rotate array to signal character position
var rotateArray = rotateToPosition(signalCharacter, indexCharacter, plainAlphabet, cipherAlphabet);
for (var count = 0; count < plainText.length; count++)
{
var singleLetter = plainText.charAt(count);
i = cipherAlphabet[singleLetter];
encryptedString = encryptedString + rotateArray[i];
}
return encryptedString;
}
function testEncryption()
{
// encrypts word JAVASCRIPT using given index characters
var codeText;
codeText = encrypt('JAVASCRIPT', 'X', 'n', plainArray, cipherArray);
window.alert(codeText);
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME = "Form">
<P>
<INPUT TYPE = "button" NAME = "testButton" VALUE ="Test Encryption"
ONCLICK = " testEncryption() ;">
</P>
</FORM>
</BODY>
</HTML>
答案 0 :(得分:2)
你错过了testEncryption()
上的结束大括号。
function testEncryption()
{
// encrypts word JAVASCRIPT using given index characters
var codeText;
codeText = encrypt('JAVASCRIPT', 'X', 'n', plainArray, cipherArray);
window.alert(codeText);
} // <-- See here
这没有说明代码是否符合您打算它要执行的操作。但是,当您单击按钮时会发生一些事情。
注意,使用Javascript控制台检查错误。 Firebug是一个很好的,Chrome也附带一个。
答案 1 :(得分:1)
我可以在代码中看到两个明显的错误。由于这可能是家庭作业,我只会提供检查指示:
还有一条评论 - 在您获得代码工作之后:逐个旋转数组,直到它正确为止,这是一种非常缓慢的方式来获得您想要的东西。您可能希望实现“旋转n”功能。当然,您还需要编写一个函数来计算n
值是什么。
答案 2 :(得分:0)
}
中没有结束大括号(testEncryption
)。这是一个开始。