2013-01-16 16 views
0

這是我的代碼。這是德國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

在'var code = []'之後和'document.simulator.rotor3.value = alphabet [newPos]'和'workingMessage =「」' –

+0

@AlexW之後缺少';'但很抱歉導致目前的問題。任何其他想法? – ChapmIndustries

+0

@ChapmIndustries如果你的代碼很好而且健壯,那麼發現錯誤會更容易。有時候一個簡單的分號,逗號等可以成爲Javascript引擎中的扳手。我會建議匹配你的括號,看看你在Javascript控制檯上得到的輸出。 –

回答

0

我不知道你希望發生的事情,但也有一些缺陷代碼:

  • JavaScript字符串值是不可變的 - 他們是沒有對象。調用函數如toUpperCase不會更改變量,但返回的新值。因此,document.simulator.encoder.value.toUpperCase()message.toUpperCase()是無用的。
  • 缺少分號後var code = [](儘管它不應該的問題,你永遠無法確定)
  • toWorkWith缺少var聲明 - 似乎並沒有被預期
  • for(var char in messageArray) - 從來沒有列舉陣列性能!使用for循環迭代其指紋(請參閱Why is using "for...in" with array iteration a bad idea?
  • for(var letter in message) - 由於message是一個字符串,因此不要嘗試枚舉其屬性! a)這是不是在IE中工作b)你可能會捕獲String.prototype enumerable屬性。相反,使用正常的循環和message.length
  • 遍及你的腳本,你在混合代碼邏輯和DOM訪問。您是否使用DOM來檢索,顯示和存儲值?分3步進行:從DOM讀取輸入,執行邏輯,寫入輸出。
+0

第三個項目符號可能是問題 –

+0

我沒有發佈與此一致的html,但是我在HTML表單中存儲變量。這是一個互動的事物,這樣用戶就可以在他們眼前看到事物的變化。 – ChapmIndustries

+0

當然,但這並不意味着你應該將邏輯和表示混合在一起。使用MVC模式。 – Bergi

相關問題