2015-02-24 98 views
0

我有一個website有一個textarea和一個按鈕。我希望用戶能夠將文本放入textarea中,並在按下按鈕時,在爲其設置的div中獲取一串二進制文件。For循環無法比較陣列的鍵和顯示結果

問題是它凍結了,從不顯示結果。我做:

$(document).ready(function(){ 
    var binaryAlphabet = "00100000 00100001 00100010 00100011 00100100 00100101 00100110 00100111 00101000 00101001 00101010 00101011 00101100 00101101 00101110 00101111 00110000 00110001 00110010 00110011 00110100 00110101 00110110 00110111 00111000 00111001 00111010 00111011 00111100 00111101 00111110 00111111 01000000 01000001 01000010 01000011 01000100 01000101 01000110 01000111 01001000 01001001 01001010 01001011 01001100 01001101 01001110 01001111 01010000 01010001 01010010 01010011 01010100 01010101 01010110 01010111 01011000 01011001 01011010 01011011 01011100 01011101 01011110 01011111 01100000 01100001 01100010 01100011 01100100 01100101 01100110 01100111 01101000 01101001 01101010 01101011 01101100 01101101 01101110 01101111 01110000 01110001 01110010 01110011 01110100 01110101 01110110 01110111 01111000 01111001 01111010 01111011 01111100 01111101 01111110 0001010"; 
    var binaryAlphabet = binaryAlphabet.split(" "); 

    var alphabet = [" ","!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~","\n"]; 

    //!"#$%&'()*+,-./:;<=>[email protected][\]^_`abcdefghijklmnopqrstuvwxyz{|}~ 

    var i, j; 
    var toBinaryString = ""; 
    var toBinaryArray = []; 
    var toTextString = ""; 
    var toTextArray = []; 
    var valstring = ""; 
    var outcome = ""; 

    $('#text-button').click(function(){ 
     outcome = ""; 
     toBinaryString = $('#text-input').val(); 
     toBinaryArray = toBinaryString.split(""); 

     for (i = 0; i < toBinaryString.length; i++){ 
      for (j = 0; j < binaryAlphabet.length;){ 
       if (toBinaryString[i] === binaryAlphabet[j]){ 
        outcome += binaryAlphabet[j]; 
       } 
      } 
     } 
     $("#page-output").append(outcome + ""); 
    }); 
}); 

和:

<body> 
<div id="page-header"></div> 
<div id="page-container"> 
    <div id="page-input"> 
     <h2>Binary Converter</h2> 
     <br> 
     Text to Binary:<br> 
     <textarea id="text-input" rows="5" cols="22" placeholder="Enter text to convert" style="resize:none"></textarea> 
     <button id="text-button">Convert to Binary</button> 
     <br><br><br><br><br> 
     Binary to Text:<br> 
     <textarea id="binary-input" rows="5" cols="22" placeholder="Enter binary to convert" style="resize:none"></textarea> 
     <button id="binary-button">Convert to Text</button> 
    </div> 
    <div id="page-output"></div> 
</div> 
</body> 

http://www.codecademy.com/JamesTill/codebits/ixFpA5/edit

+2

哦,我想是的。而且我猜它比我想象的更容易。有9個小時的生活我不會回來。 – 2015-02-24 09:33:19

回答

0

錯誤
1.增量j在內環
2.語義錯誤:必須使用alphabet而不是binaryAlphabet作爲內環

鍵查找

還可以使用控制檯記錄Chrome開發者工具突破點進行調試。

因此,我改變的代碼部分FF:

$('#text-button').click(function(){ 
     console.log("click function...") 
     outcome = ""; 
     toBinaryString = $('#text-input').val(); 
     toBinaryArray = toBinaryString.split(""); 

     for (i = 0; i < toBinaryString.length; i++){ 
      console.log("loop 1...") 
      for (j = 0; j < alphabet.length;++j){ 
       console.log("loop 2...") 
       if (toBinaryString[i] === alphabet[j]){ 
        outcome += binaryAlphabet[j]; 
       } 
      } 
     } 
     console.log("appending...") 
     $("#page-output").append(outcome + ""); 
     console.log("appended...") 
    }); 

建議:
變化字母排列到一張地圖,這樣查詢可以快速爲您服務。 這樣的:

var alphabet = {" ":" ","!":"!",'"':'"',"#":"#",..