简体   繁体   English

使用javascript(带有ASCII码)的html中的Caesar Cipher需要帮助

[英]Caesar Cipher in html using javascript (with ASCII codes) help required

I'm having trouble getting this code to work. 我无法使此代码正常工作。 Am more familiar with python - new to javascript. 更熟悉python-javascript新功能。 This was what my python equivalent looked like: 这就是我的python等效形式:

   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)

and below is the javascript version - I've used comments. 以下是javascript版本-我使用了注释。 I've set it up a bit differently as well. 我对它的设置也有所不同。 (the last block is the html) For some reason, it's only showing the button and input boxes - but the output is not being displayed. (最后一个块是html)由于某种原因,它仅显示按钮和输入框-但未显示输出。

Thanks for the help 谢谢您的帮助

 <script> 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){ // 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 = "?"; } } encrypt += String.fromCharCode(temp) } //output to the page 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> 

You have one open curly bracket and one closing curly bracket too much. 您有一个大括号和一个大括号。 See comment. 见评论。

 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> 

I created a working example here: https://jsfiddle.net/w4rafn1j/2/ 我在这里创建了一个工作示例: https : //jsfiddle.net/w4rafn1j/2/

The following line needs a semicolon after it instead of a block, because it is a function call: 下一行在其后需要一个分号而不是一个块,因为它是一个函数调用:

temp=userName.charCodeAt(i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM