Javascript 当我运行我的程序时,我的内存正在耗尽。有没有更好的方法,或者我正在创建无限循环?
我试着创造这个密码来破译凯撒的东西。我知道我的拼写很糟糕,但这个项目只是为了好玩,我还不太懂编程 这是我的密码:Javascript 当我运行我的程序时,我的内存正在耗尽。有没有更好的方法,或者我正在创建无限循环?,javascript,html,Javascript,Html,我试着创造这个密码来破译凯撒的东西。我知道我的拼写很糟糕,但这个项目只是为了好玩,我还不太懂编程 这是我的密码: <!DOCTYPE html> <html> <head> <title> </title> </head> <body> <script> var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"
<!DOCTYPE html>
<html>
<head>
<title> </title>
</head>
<body>
<script>
var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",]
var input = prompt("Enter In The Text That You Want To Decode")
var results = []
//Decoding
var ceaser_alphabet = []
var cAlphabet_creation_progress = 1
var ceaser_progress = 1
var decoding_progress = 1
var decoded_message = ""
var slice1 = []
var slice2 = []
var decoder = function () {
//Uses 25 instead of 26 because the first item in an array is counted as "0"
slice1 = alphabet.slice(0, (26 - ceaser_progress))
slice2 = alphabet.slice((26 - ceaser_progress), 26)
while (cAlphabet_creation_progress < 26) {
if (cAlphabet_creation_progress < slice1.length) {
ceaser_alphabet.push(slice1[cAlphabet_creation_progress])
} else {
ceaser_alphabet.push(slice2[cAlphabet_creation_progress])
}
}
//Decoding
while(decoding_progress < input.length) {
if (!input.charAt(decoding_progress) == " ") {
decoded_message = decoded_message + ceaser_alphabet[ceaser_alphabet.indexOf(alphabet.indexOf(input.charAt(decoding_progress)))]
decoding_progress++
} else {
decoded_message = decoded_message + " "
}
}
if (decoding_progress == input.length) {
results.push(decoded_message)
ceaser_progress++
decoding_progress = 1
ceaser_alphabet = []
decoded_message = ""
}
}
var possibilities_left = 26
while (possibilities_left > 0) {
possibilities_left--
decoder()
}
console.log(results)
</script>
</body>
</html>
变量字母表=[“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”、“i”、“j”、“k”、“l”、“m”、“n”、“o”、“p”、“q”、“r”、“s”、“t”、“u”、“v”、“w”、“x”、“y”、“z”]
var input=prompt(“输入要解码的文本”)
var结果=[]
//解码
var ceaser_字母表=[]
var cAlphabet_创建_进度=1
var ceaser_进度=1
var解码_进度=1
var decoded_message=“”
变量1=[]
变量2=[]
var解码器=函数(){
//使用25而不是26,因为数组中的第一项计为“0”
切片1=字母表切片(0,(26-ceaser_进度))
切片2=字母表切片((26-ceaser_进度),26)
同时(cAlphabet_创建_进度<26){
如果(cAlphabet_创建_进度<切片1.长度){
ceaser_字母表推送(切片1[字母表创建进度])
}否则{
ceaser_字母表推送(切片2[字母表创建进度])
}
}
//解码
while(解码进度<输入长度){
如果(!input.charAt(解码进度)==“”){
解码信息=解码信息+ceaser字母表[ceaser字母表.indexOf(字母表.indexOf(输入.charAt(解码过程)))]
解码进程++
}否则{
解码消息=解码消息+“”
}
}
if(解码进度==输入长度){
结果.推送(解码的_消息)
凯瑟鲁进展++
解码进度=1
ceaser_字母表=[]
解码的_消息=“”
}
}
var可能性_左=26
而(可能性_左>0){
你离开的可能性--
解码器()
}
console.log(结果)
当我在chrome上运行它时,它表示内存不足。另一个问题可能是,您不应该在chrome上执行此操作。我认为这是您的第一个
,而语句是无限循环的,因为变量cAlphabet\u creation\u progress
没有递增
while (cAlphabet_creation_progress < 26) {
if (cAlphabet_creation_progress < slice1.length) {
ceaser_alphabet.push(slice1[cAlphabet_creation_progress])
} else {
ceaser_alphabet.push(slice2[cAlphabet_creation_progress])
}
cAlphabet_creation_progress++ //needs to be incremented
}
while(cAlphabet_创建_进度<26){
如果(cAlphabet_创建_进度<切片1.长度){
ceaser_字母表推送(切片1[字母表创建进度])
}否则{
ceaser_字母表推送(切片2[字母表创建进度])
}
cAlphabet_creation_progress++//需要递增
}
我相信这里的问题是您的第一个,语句无限循环,因为变量cAlphabet\u creation\u progress
没有递增
while (cAlphabet_creation_progress < 26) {
if (cAlphabet_creation_progress < slice1.length) {
ceaser_alphabet.push(slice1[cAlphabet_creation_progress])
} else {
ceaser_alphabet.push(slice2[cAlphabet_creation_progress])
}
cAlphabet_creation_progress++ //needs to be incremented
}
while(cAlphabet_创建_进度<26){
如果(cAlphabet_创建_进度<切片1.长度){
ceaser_字母表推送(切片1[字母表创建进度])
}否则{
ceaser_字母表推送(切片2[字母表创建进度])
}
cAlphabet_creation_progress++//需要递增
}
我知道这不是您最初的问题,但我认为您的decoder类会有问题。除非我误读了您的代码,否则条件if语句:
if (!input.charAt(decoding_progress) == " ") {
应该是:
if (input.charAt(decoding_progress) != " ")
如果您试图检查它是否是空格。我知道这不是您最初的问题,但我认为您的decoder类会有问题。除非我误读了您的代码,否则条件if语句:
if (!input.charAt(decoding_progress) == " ") {
应该是:
if (input.charAt(decoding_progress) != " ")
如果您试图检查它是否为空格。内存不足通常表示无限循环。检查所有循环的条件。内存不足通常表示无限循环。检查你所有循环的条件。我相信你在这里也是正确的!应该只放在布尔变量前面以检查是否为假。谢谢@davidele,我应该对我的答案更加自信一点!我相信你在这里也是对的!应该只放在布尔变量前面以检查是否为假。谢谢@davidele,我应该对我的答案更加自信一点!谢谢你的帮助,我现在觉得自己很笨没问题,从来没有觉得自己很笨只是不太了解情况。谢谢你的帮助,我现在觉得自己很笨没问题,从来没有觉得自己很笨只是不太了解情况。