Caesar密码在html中使用javascript(带有ASCII代码)需要帮助
Caesar Cipher in html using javascript (with ASCII codes) help required
我很难让这些代码正常工作。我更熟悉python-javascript新手。这就是我的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)
下面是javascript版本-我使用了注释。我的设置也有点不同。(最后一个块是html)出于某种原因,它只显示按钮和输入框,但没有显示输出。
感谢的帮助
<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>
你有一个开花括号和一个闭花括号太多了。请参阅评论。
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>
我在这里创建了一个工作示例:https://jsfiddle.net/w4rafn1j/2/
以下行后面需要分号而不是块,因为它是一个函数调用:
temp=userName.charCodeAt(i);
相关文章:
- 从桌面读取python文件时高亮显示代码
- 如何将函数包装在函数中以避免代码重复
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 货币代码为欧元-金额的格式不应包含小数
- Regex代码只允许一个空格
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 从var向代码隐藏函数传递值
- 如何动态插入jquery代码
- 如何在php变量中嵌入JQuery代码
- Caesar密码在html中使用javascript(带有ASCII代码)需要帮助
- 如何在 Javascript 中获取大写小写的 AsCII 代码
- 使用 ASCII 代码关闭选项卡
- 在 JavaScript 中返回错误的 ASCII 代码
- 将ASCII代码存储在mysql数据库中
- Sencha-选择字段选项中的HTML ASCII代码
- js警报框中的资源文件变量给出字符串ASCII代码问题
- 用JavaScript解码字符串中的混合ASCII代码
- 如何在JavaScript中替换ASCII代码(alt+207)
- 表单上的编码问题;一些特殊字符显示为 ASCII 代码
- 如何防止加号在邮件正文中编码为 ASCII 代码