2016-03-25 8 views
0

このコードが正しく機能するようになっています。 Pythonにもっと精通しています - 新しいjavascript。これは私のpythonの同等のもののようだった:javascript(ASCIIコード付き)を使用したhtmlのCaesar Cipherが必要です

userinput = input("Enter the message you want to encryppt: ") 
    shift = int(input("How many letters do you want to shift by? ")) 
    empty = "" 

    for char in userinput: 
    a = ord('a') if char.islower() else ord('A') 
    if char.isalpha(): 
    firstord = ord(char) - a 
    realord = firstord + shift 
    realord = realord % 26 
    realord = realord + a 
    alpha = (chr(realord)) 
    empty = empty + alpha 
    else: 
    notalpha = ("?") 
    empty = empty + notalpha 
    print(empty) 

以下は、JavaScriptのバージョンです - 私はコメントを使用しました。私はちょっと別にそれを設定しました。 (最後のブロックはhtmlです)何らかの理由で、ボタンと入力ボックスしか表示されませんが、出力は表示されません。ヘルプ

<script> 
 

 
function enterName(){ 
 

 

 
\t var userName = document.getElementById("word_in").value;  //get the input string from the page 
 
\t var shiftBy = Number(document.getElementById("shift").value); \t //get the amount of shift and convert it into a number. This Is IMPORTANT 
 
\t var size= userName.length; \t \t \t \t \t \t \t \t \t \t //get the size of the input string 
 
\t var encrypt=""; 
 
\t var temp = 0; 
 
\t var i=0; 
 
\t 
 
\t //step through the string 1 character at a time 
 
\t for (i=0; i<size; i++){ 
 
\t 
 
\t //store the ASCII value of each character 
 
\t \t temp=userName.charCodeAt(i){ 
 

 
\t \t // Uppercase 
 
\t \t if ((temp >= 65) && (temp <= 90)){ 
 
\t \t \t temp = (((temp - 65 + shiftBy) % 26) + 65); 
 
\t \t } 
 

 
\t \t // Lowercase 
 
\t \t else if ((temp >= 97) && (temp <= 122)){ 
 
\t \t \t temp = (((temp - 97 + shiftBy) % 26) + 97); 
 
\t \t } 
 

 
\t \t else { 
 
\t \t \t temp = "?"; 
 
\t \t } 
 
\t \t \t 
 
\t \t } 
 
\t \t encrypt += String.fromCharCode(temp) \t 
 
\t } \t 
 
\t \t 
 
\t //output to the page 
 
\t document.getElementById("word_out").innerHTML = encrypt; 
 

 
} 
 

 
</script>
<body> 
 
<p>Please enter your name:</p> 
 
<input id="word_in"> 
 
<p>Please enter the shift:</p> 
 
<input id = "shift"> 
 
<button type = "button" onclick="enterName()"></button> 
 
<p id = "word_out"></p> 
 
</body>

答えて

1

ため

おかげであなたは1つの、オープン中括弧、1つはあまりにも多くの中括弧を閉じています。コメントを参照してください。

function enterName() { 
 
    var userName = document.getElementById("word_in").value;  //get the input string from the page 
 
    var shiftBy = Number(document.getElementById("shift").value); //get the amount of shift and convert it into a number. This Is IMPORTANT 
 
    var size = userName.length;          //get the size of the input string 
 
    var encrypt = ""; 
 
    var temp = 0; 
 
    var i = 0; 
 

 
    //step through the string 1 character at a time 
 
    for (i = 0; i < size; i++) { 
 

 
     //store the ASCII value of each character 
 
     temp = userName.charCodeAt(i); //{ <------------------------------------ too much 
 

 
     // Uppercase 
 
     if ((temp >= 65) && (temp <= 90)) { 
 
      temp = (((temp - 65 + shiftBy) % 26) + 65); 
 
     } 
 

 
      // Lowercase 
 
     else if ((temp >= 97) && (temp <= 122)) { 
 
      temp = (((temp - 97 + shiftBy) % 26) + 97); 
 
     } 
 

 
     else { 
 
      temp = "?"; 
 
     } 
 

 
     //} <------------------------------------------------------------------- too much 
 
     encrypt += String.fromCharCode(temp); 
 
    } 
 

 
    //output to the page 
 
    document.getElementById("word_out").innerHTML = encrypt; 
 
}
<p>Please enter your name:</p> 
 
<input id="word_in"> 
 
<p>Please enter the shift:</p> 
 
<input id="shift"> 
 
<button type="button" onclick="enterName()"></button> 
 
<p id="word_out"></p>

+0

それはそれのような単純なものかもしれないと思った - ありがとう – user6113119

0

私はここで作業例を作成しました: https://jsfiddle.net/w4rafn1j/2/

を、それが関数呼び出しであるため、次の行には、代わりにブロックのそれの後にセミコロンを必要とします:

temp=userName.charCodeAt(i); 
関連する問題