gpt4 book ai didi

javascript - 使用 javascript(带有 ASCII 代码)的 html 凯撒密码需要帮助

转载 作者:行者123 更新时间:2023-12-03 07:20:53 25 4
gpt4 key购买 nike

我无法让此代码正常工作。我更熟悉 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 版本 - 我使用了注释。我的设置也有点不同。 (最后一个 block 是 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>

关于javascript - 使用 javascript(带有 ASCII 代码)的 html 凯撒密码需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36216785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com