数组中的Javascript标点正在返回';未定义';
我正在尝试写一个小密码程序,需要将单词和标点符号组合在一起。如果我使用字母/数字/特殊字符,代码将非常有效!通过),但不使用逗号、句点或问号。我检查了,代码返回未定义,但只针对这三个标点。我写了这段代码的前一个版本,去掉了所有工作正常的标点符号,现在我正试图再次添加标点符号数组中的Javascript标点正在返回';未定义';,javascript,arrays,undefined,punctuation,Javascript,Arrays,Undefined,Punctuation,我正在尝试写一个小密码程序,需要将单词和标点符号组合在一起。如果我使用字母/数字/特殊字符,代码将非常有效!通过),但不使用逗号、句点或问号。我检查了,代码返回未定义,但只针对这三个标点。我写了这段代码的前一个版本,去掉了所有工作正常的标点符号,现在我正试图再次添加标点符号 var alpha = ["A", "B", "C", "D", "E", "F", "G&q
var alpha = ["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", "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", " ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", ",", ".", "?"];
else if (oldLet == "!") {
index = 63;
keyIndex = keyIndex - 1;
} else if (oldLet == ".") {
index = 84;
keyIndex = keyIndex - 1;
} else if (oldLet == "?") {
index = 85;
keyIndex = keyIndex - 1;
}
var newLet = alpha[index];
alert(newLet);
cipherArray.push(newLet);
}
cipherArray = cipherArray.join("");
document.getElementById("output").innerHTML = cipherArray;
}
我完全搞不懂为什么代码可以完美地处理字母、数字和特殊字符,但却不能正确处理标点符号。感谢您的帮助。伙计,我想您弄错了索引
else if(oldLet == ","){
alert("got here 2" + oldLet);
index = 83;
alert("got here 3" + alpha[index]);
keyIndex = keyIndex -1;
}
else if(oldLet == "."){
index = 84;
keyIndex = keyIndex -1;
}
else if(oldLet == "?"){
index = 85;
keyIndex = keyIndex -1;
}
看,不是83、84、85,而是73、74和75
您可以检查以下操作:
alert(alpha.indexOf(","));
与其手动设置索引,为什么不创建一个查找映射呢
constindexbychar=newmap(alpha.Map((char,index)=>[char,index]))
然后可以执行index=indexByChar.get(oldLet)
。