这是我的代码。这是德国Enigma机器的草稿。我正在尝试发出消息,我将实际通过机器运行,除非它在第二次运行该函数之前不会创建该消息。奇怪的是我知道函数运行是因为我看到它的部分执行,但就代码而言toWorkWith
在第一次运行时为空并在第二次运行时填充?
function encode(){
var alphabet = ["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 rotor_1 = {"A":["E"],"B":["K"],"C":["M"],"D":["F"],"E":["L"],"F":["G"],"G":["D"],"H":["Q"],"I":["V"],"J":["Z"],"K":["N"],"L":["T"],"M":["O"],"N":["W"],"O":["Y"],"P":["H"],"Q":["X"],"R":["U"],"S":["S"],"T":["P"],"U":["A"],"V":["I"],"W":["B"],"X":["R"],"Y":["C"],"Z":["J"]};
var rotor_2 = {"A":["A"],"B":["J"],"C":["D"],"D":["K"],"E":["S"],"F":["I"],"G":["R"],"H":["U"],"I":["X"],"J":["B"],"K":["L"],"L":["H"],"M":["W"],"N":["T"],"O":["M"],"P":["C"],"Q":["Q"],"R":["G"],"S":["Z"],"T":["N"],"U":["P"],"V":["Y"],"W":["F"],"X":["V"],"Y":["O"],"Z":["E"]};
var rotor_3 = {"A":["B"],"B":["D"],"C":["F"],"D":["H"],"E":["J"],"F":["L"],"G":["C"],"H":["P"],"I":["R"],"J":["T"],"K":["X"],"L":["V"],"M":["Z"],"N":["N"],"O":["Y"],"P":["E"],"Q":["I"],"R":["W"],"S":["G"],"T":["A"],"U":["K"],"V":["M"],"W":["U"],"X":["S"],"Y":["Q"],"Z":["O"]};
var reflector = {"A":["A"],"B":["B"],"C":["C"],"D":["D"],"E":["E"],"F":["F"],"G":["G"],"H":["H"],"I":["I"],"J":["J"],"K":["K"],"L":["L"],"M":["M"],"N":["N"],"O":["O"],"P":["P"],"Q":["Q"],"R":["R"],"S":["S"],"T":["T"],"U":["U"],"V":["V"],"W":["W"],"X":["X"],"Y":["Y"],"Z":["Z"]};
document.simulator.encoder.value.toUpperCase();
var message = document.simulator.encoder.value.trim();
message.toUpperCase();
document.simulator.encoder.value = message.toUpperCase();
var code = []
//Turns the rotors
function updateRotorState(rotorNum){
var rotor1state = document.simulator.rotor1.value.toUpperCase();
var rotor2state = document.simulator.rotor2.value.toUpperCase();
var rotor3state = document.simulator.rotor3.value.toUpperCase();
if(rotorNum == 1){
var rotorPos = alphabet.indexOf(rotor1state);
var newPos = rotorPos + 1;
if(rotor1state == "V"){
document.simulator.rotor1.value=alphabet[newPos]
updateRotorState(2);
}
if(rotorPos == 25){
newPos = 0;
}
document.simulator.rotor1.value = alphabet[newPos];
}
if(rotorNum == 2){
var rotorPos = alphabet.indexOf(rotor2state);
var newPos = rotorPos + 1;
if(rotor2state == "E"){
document.simulator.rotor2.value = alphabet[newPos];
updateRotorState(3);
}
if(rotorPos == 25){
newPos = 0;
}
document.simulator.rotor2.value = alphabet[newPos];
}
if(rotorNum == 3){
var rotorPos = alphabet.indexOf(rotor3state);
var newPos = rotorPos + 1;
if(rotorPos == 25){
newPos = 0;
}
document.simulator.rotor3.value = alphabet[newPos]
//Eventually need to add code to make next rotor turn over
}
}
//Turns the message into a stripped output. Removes all non letter characters including spaces
function workingMessageGen(message){
var workingMessage = ""
var messageArray = message.split('');
for(var char in messageArray){
for(var letter in alphabet){
if(messageArray[char] == alphabet[letter]){
workingMessage += alphabet[letter];
}
}
}
return workingMessage;
}
toWorkWith = workingMessageGen(message);
for(var letter in message){
updateRotorState(1);
}
document.simulator.decoder.value=toWorkWith;
}
答案 0 :(得分:0)
我不确定您预期会发生什么,但代码中存在一些缺陷:
toUpperCase
之类的函数不会更改变量,但返回新值。因此,document.simulator.encoder.value.toUpperCase()
和message.toUpperCase()
无用。var code = []
之后缺少分号(虽然没关系,但你永远无法确定)toWorkWith
缺少var
声明 - 似乎没有意图for(var char in messageArray)
- 永远不会枚举数组属性!使用for循环迭代其indizes(请参阅Why is using "for...in" with array iteration a bad idea?)for(var letter in message)
- 因为message
是一个字符串,所以不要尝试枚举其属性! a)这在IE中不起作用b)您可能会在String.prototype
上捕获可枚举的属性。相反,请使用普通for循环和message.length
。